简体   繁体   中英

Deserializing JSON using Newtonsoft in C#

I have the following JSON:

[
    {
        "name": "codeURL",
        "value": "abcd"
    },
    {
        "name": "authURL",
        "value": "fghi"
    }
]

I created the following objects:

public class ConfigUrlModel {
    [JsonProperty("name")]
    public abstract string name { get; set; }
    [JsonProperty("value")]
    public abstract string value { get; set; }
}

public class ConfigUrlsModel {
    [JsonProperty]
    public List<ConfigUrlModel> ConfigUrls { get; set; }
}

I am deserializing with the following line:

resultObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigUrlsModel>(resultString);
ConfigUrlsModel result = resultObject as ConfigUrlsModel;

I am getting the following error:

Exception JsonSerializationException with no inner exception: Cannot deserialize JSON array into type 'Microsoft.Xbox.Samples.Video.Services.Jtv.Models.ConfigUrl.ConfigUrlsModel'.
Exception JsonSerializationException with no inner exception: Cannot deserialize JSON array into type 'Microsoft.Xbox.Samples.Video.Services.Jtv.Models.ConfigUrl.ConfigUrlsModel'.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(Type objectType, JsonContract contract)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contr   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(Type objectType, JsonContract contract)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contrNavigationService:OnNavigateToMessage PageSourceUri=/Microsoft.Xbox.Sample.UI;component/ErrorPrompt/ErrorPromptView.xaml

What am I doing wrong? How do I fix this?

The root JSON container is an array, not an object, so deserialize it thusly:

var configUrls = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ConfigUrlModel>>(resultString);
var result = new ConfigUrlsModel { ConfigUrls = configUrls }; // If you still need the root object.

A JSON array is an ordered list of values [value1, value2, ..., value] , which is what is shown in your question. Json.NET will convert .NET arrays and collections to JSON arrays , so you need to deserialize to a collection type.

The JSON you are sending is an array, but you are attempting to deserialize it to an object. Ether change your JSON so it matches the object defintion at the top level, and has a matching attribute, like this:

{
   "ConfigUrls":[
      {
         "name":"codeURL",
         "value":"abcd"
      },
      {
         "name":"authURL",
         "value":"fghi"
      }
   ]
}

Or change your deserialize call to:

var urls = DeserializeObject<List<ConfigUrlModel>>(json);

This will return a List<ConfigUrlModel> which you can either use directly, or wrap in a ConfigUrlModels instance if you need to.

Additionally it is possible to deserialize this JSON directly to the desired class, by creating a custom newtonsoft JsonConverter sublcass. But that's going to make the code a little less clear, so avoid it if possible.

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