1

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?

2
  • where is JtvConfigUrlsModel ?? Commented Oct 5, 2015 at 7:32
  • @PranavPatel The Jtv was a mistake. It's ConfigUrlsModel. Commented Oct 5, 2015 at 7:34

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.