繁体   English   中英

使用JavaScriptSerializer反序列化JSON C#-数组反序列化不支持此类型

[英]Using JavaScriptSerializer to Deserialize JSON C# - type not supported for deserialization of an array

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>和不同的数据类型之内,但我找不到解决方案。 任何建议表示赞赏。

影音

尝试直接反序列化为Class1的列表。

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

另外,不要使用Object作为类名。 这是一个非常糟糕的做法。

您可以控制JSON.NET如何对属性进行序列化/反序列化,如下所示: 使用Json.net进行序列化时,如何更改属性名称?

例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM