简体   繁体   中英

Unusual Deserialization Error from JSON to C#

.Net Fiddle 1

I have a JOSN received from external API as follows

[{
    "assignedto": "MAIN STAFF",
    "createduser": "API-71",
    "departmentid": "1",
    "observations": [{
        "abnormalflag": "abnormal",
        "analytename": "HGB A1C",
        "value": "5"
    }],
    "pages": [],
    "priority": "2",
    "status": "REVIEW"
}]

I did a Paste Special in Visual Studio and got following classes

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

public class Class1
{
    public string assignedto { get; set; }
    public string createduser { get; set; }
    public string departmentid { get; set; }
    public Observation[] observations { get; set; }
    public object[] pages { get; set; }
    public string priority { get; set; }
    public string status { get; set; }
}

public class Observation
{
    public string abnormalflag { get; set; }
    public string analytename { get; set; }
    public string value { get; set; }
}

When I do a deserialization, I am getting following error

Run-time exception (line 24): Cannot deserialize the current JSON array (eg [1,2,3]) into type 'Rootobject' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

C# Code

public static void Main(string[] args) 
{

    var json = @"[{
                    ""assignedto"": ""MAIN ST (HUB) STAFF"",
                    ""createduser"": ""API-7127"",
                    ""departmentid"": ""1"",
                    ""observations"": [{
                        ""abnormalflag"": ""abnormal"",
                        ""analytename"": ""HGB A1C"",
                        ""value"": ""5""
                    }],
                    ""pages"": [],
                    ""priority"": ""2"",
                    ""status"": ""REVIEW""
                }]";



    Rootobject resultObj = JToken.Parse(json).ToObject<Rootobject>();

}

I referred similar questions like Create a strongly typed c# object from json object with ID as the name - but that is a different issue.

Any idea how to fix this? Also what is the better way to generate C# classes from JSON?

Note: I also tried with class I got from http://json2csharp.com/ . That also faield - Fidlle 2

I would use Newtonsoft / Json convert and change this:

Rootobject resultObj = JToken.Parse(json).ToObject<Rootobject>();

to:

using Newtonsoft.Json;
-- snip --
    var resultObj = JsonConvert.DeserializeObject<List<Class1>>(json);

    Console.WriteLine(resultObj.Count); // 1
    Class1 result = resultObj[0];
    Console.WriteLine(result.assignedto); // "MAIN ST (HUB) STAFF"

This will give you a collection of RootObject

As @JeffMeracdo states above - you are providing a collection of object and trying to parse as though it is a single object

As @JeffMeracdo states above, try this:

List<Example> resultObj = JsonConvert.DeserializeObject<List<Example>>(json);

Following using statement and package along with Newtonsoft.Json Nuget package:

using Newtonsoft.Json;

Classes:

public class Observation
{
    public string abnormalflag { get; set; }
    public string analytename { get; set; }
    public string value { get; set; }
}

public class Example
{
    public string assignedto { get; set; }
    public string createduser { get; set; }
    public string departmentid { get; set; }
    public List<Observation> observations { get; set; }
    public List<object> pages { get; set; }
    public string priority { get; set; }
    public string status { get; set; }
}

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