简体   繁体   中英

Deserialize JSON object with null arrays using newtonsoft.json

I am hitting a web service that returns a JSON-like response, and I am trying to deserialize the response to C# objects. I hused the Visual Studio Edit > Paste Special > Paste JSON as Classes method to create the base version of the classes. When I try to deserialize the web service response to those classes using the method below (where rawResp is the response from the web service as text)

var results = JsonConvert.DeserializeObject<List<ApplyEditsResult>>(rawResp);

i get the following error message:

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'Rid20Api.Models.ApplyEditsResult+Result' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '[0].addResults[0].objectId', line 1, position 35.

I have tried adding "JsonObject" attribute to the appropriate classes; added the JsonArray attribute as well. What am i missing to correctly deserialize the response?

The response from the web service:

[
  {
    "id": 0,
    "addResults": [
      {
        "objectId": 1215,
        "globalId": "{5a3d5c49-856c-4de4-9d20-c7abf0e070c8}",
        "success": true
      }
    ],
    "attachments": {
      "addResults": [],
      "deleteResults": [],
      "updateResults": []
    }
  },
  {
    "id": 2,
    "addResults": [
      {
        "objectId": 819,
        "globalId": "{459f456a-c115-4f9c-b1f9-d754d55980dd}",
        "success": true
      },
      {
        "objectId": 820,
        "globalId": "{f690cded-2ffb-4301-ab6e-58ae9733eeba}",
        "success": true
      }
    ]
  }
]

and the classes (it hasn't made any difference whether the classes are nested or are 'stand-alone'):

    public class ApplyEditsResult
    {

        public int id { get; set; }
        public Result[] addResults { get; set; }
        public Result[] updateResults { get; set; }
        public Result[] attachments { get; set; }

        [JsonObject]
        public class Attachments
        {
            [JsonProperty("addResults")]
            public object[] addResults { get; set; }
            [JsonProperty("deleteResults")]
            public object[] deleteResults { get; set; }
            [JsonProperty("updateResults")]
            public object[] updateResults { get; set; }
        }

        [JsonArray(AllowNullItems = true)]
        [JsonObject]
        public class Result
        {
            public bool success { get; set; }
            public Error error { get; set; }
            public int objectId { get; set; }
            public string globalId { get; set; }
        }

        [JsonObject]
        public class Error
        {
            public int code { get; set; }
            public string description { get; set; }
        }

    }

EDIT ......... Here is the unedited collection of classes created by the VS tools. As noted above and in the comments below, i changed some of these classes 1) to better fit my code (including error responses from the service) and 2) in an (failed) attempt to correct the bug myself.

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

public class Class1
{
    public int id { get; set; }
    public Addresult[] addResults { get; set; }
    public Attachments attachments { get; set; }
}

public class Attachments
{
    public object[] addResults { get; set; }
    public object[] deleteResults { get; set; }
    public object[] updateResults { get; set; }
}

public class Addresult
{
    public int objectId { get; set; }
    public string globalId { get; set; }
    public bool success { get; set; }
}

Your class should look like this

public class AddResult
{

    [JsonProperty("objectId")]
    public int objectId { get; set; }

    [JsonProperty("globalId")]
    public string globalId { get; set; }

    [JsonProperty("success")]
    public bool success { get; set; }
}

public class Attachments
{

    [JsonProperty("addResults")]
    public List<object> addResults { get; set; }

    [JsonProperty("deleteResults")]
    public List<object> deleteResults { get; set; }

    [JsonProperty("updateResults")]
    public List<object> updateResults { get; set; }
}

public class ApplyEditsResult
{

    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("addResults")]
    public List<AddResult> addResults { get; set; }

    [JsonProperty("attachments")]
    public Attachments attachments { get; set; }
}

and then

var results = JsonConvert.DeserializeObject<List<ApplyEditsResult>>(rawResp);

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