简体   繁体   中英

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

New to C# and JSON but basically I have to use a C# script component in SSIS to extract data from a web service. I was able to do it initially with a different JSON format but this one includes an array. I get the following error:

"Type 'Rootobject' is not supported for deserialization of an array."

Below is how I call the Deserializer:

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

Here is my JSON structure (not quite all since fairly large):

[  
   {  
      "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":[  

         ]
      }
   }
]

Finally here is my Class structure obtained through Paste Special in 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; }
}

I know the problem lies within how I call the Deserialize<Rootobject> and different data types but I wasn't able to find the solution. Any suggestions are appreciated.

AV

Try deserializing directly to a list of Class1 .

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

Also, don't use Object as your class name. That's a really bad practice.

You can control how JSON.NET serializes/deserializes a property as shown here: How can I change property names when serializing with Json.net?

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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