首页 > 解决方案 > 使用 JavaScriptSerializer 反序列化 JSON C# - 数组的反序列化不支持类型

问题描述

C# 和 JSON 的新手,但基本上我必须在 SSIS 中使用 C# 脚本组件从 Web 服务中提取数据。我最初可以使用不同的 JSON 格式来完成它,但是这个包含一个数组。我收到以下错误:

“数组的反序列化不支持类型 'Rootobject'。”

下面是我如何调用反序列化器:

//Deserialize our JSON
 JavaScriptSerializer sr = new 
 jsonResponse = sr.Deserialize<Rootobject>(responseFromServer);

这是我的 JSON 结构(不是全部,因为相当大):

[  
   {  
      "status":"SUCCESS",
      "object":{  
         "responseStatusCode":0,
         "productId":"35100003",
         "cansimId":"251-0008",
         "cubeTitleEn":"Average counts of young persons in provincial and territorial correctional services",
         "cubeTitleFr":"Comptes moyens des adolescents dans les services correctionnels provinciaux et territoriaux",
         "cubeStartDate":"1997-01-01",
         "cubeEndDate":"2017-01-01",
         "frequencyCode":12,
         "nbSeriesCube":174,
         "nbDatapointsCube":3468,
         "releaseTime":"2019-05-09T08:30",
         "archiveStatusCode":"2",
         "archiveStatusEn":"CURRENT - a cube available to the public and that is current",
         "archiveStatusFr":"ACTIF - un cube qui est disponible au public et qui est toujours mise a jour",
         "subjectCode":[  
            "350102",
            "4211"
         ],
         "surveyCode":[  
            "3313"
         ],
         "dimension":[  ],
         "footnote":[  ],
         "correction":[  

         ]
      }
   }
]

最后这是我在 Visual Studio 中通过选择性粘贴获得的类结构:

public class Rootobject
{

    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string status { get; set; }
    public Object _object { get; set; }
}

public class Object
{
    public int responseStatusCode { get; set; }
    public string productId { get; set; }
    public string cansimId { get; set; }
    public string cubeTitleEn { get; set; }
    public string cubeTitleFr { get; set; }
    public string cubeStartDate { get; set; }
    public string cubeEndDate { get; set; }
    public int frequencyCode { get; set; }
    public int nbSeriesCube { get; set; }
    public int nbDatapointsCube { get; set; }
    public string releaseTime { get; set; }
    public string archiveStatusCode { get; set; }
    public string archiveStatusEn { get; set; }
    public string archiveStatusFr { get; set; }
    public string[] subjectCode { get; set; }
    public string[] surveyCode { get; set; }
    public Dimension[] dimension { get; set; }
    public Footnote[] footnote { get; set; }
    public object[] correction { get; set; }
}

public class Dimension
{
    public int dimensionPositionId { get; set; }
    public string dimensionNameEn { get; set; }
    public string dimensionNameFr { get; set; }
    public bool hasUom { get; set; }
    public Member[] member { get; set; }
}

public class Member
{
    public int memberId { get; set; }
    public int? parentMemberId { get; set; }
    public string memberNameEn { get; set; }
    public string memberNameFr { get; set; }
    public string classificationCode { get; set; }
    public string classificationTypeCode { get; set; }
    public int? geoLevel { get; set; }
    public int? vintage { get; set; }
    public int terminated { get; set; }
    public int? memberUomCode { get; set; }
}

public class Footnote
{
    public int footnoteId { get; set; }
    public string footnotesEn { get; set; }
    public string footnotesFr { get; set; }
    public Link link { get; set; }
}

public class Link
{
    public int footnoteId { get; set; }
    public int dimensionPositionId { get; set; }
    public int memberId { get; set; }
}

我知道问题在于我如何调用Deserialize<Rootobject>不同的数据类型,但我无法找到解决方案。任何建议表示赞赏。

视听

标签: c#arraysjsonssisjavascriptserializer

解决方案


尝试直接反序列化为Class1.

jsonResponse = sr.Deserialize<List<Class1>>(responseFromServer);

另外,不要Object用作您的班级名称。这是一个非常糟糕的做法。

您可以控制 JSON.NET 如何序列化/反序列化属性,如下所示:How can I change property names when serializing with Json.net?

例如

[JsonProperty(PropertyName = "object")]
public class MyObject
{
}

推荐阅读