I have the following json string
[["{\"object\":\"user\",\"entry\":[{\"uid\":\"823602904340066\",\"id\":\"823602904340066\",\"time\":1429276535,\"changed_fields\":[\"feed\"]}]}"],["{\"object\":\"user\",\"entry\":[{\"uid\":\"10152579760466676\",\"id\":\"10152579760466676\",\"time\":1429278530,\"changed_fields\":[\"feed\"]}]}"],["{\"object\":\"user\",\"entry\":[{\"uid\":\"10203227586595390\",\"id\":\"10203227586595390\",\"time\":1429278537,\"changed_fields\":[\"feed\"]}]}"],["{\"object\":\"user\",\"entry\":[{\"uid\":\"10203227586595390\",\"id\":\"10203227586595390\",\"time\":1429278521,\"changed_fields\":[\"feed\"]}]}"]]
JsonConvert.DeserializeObject<List<RootObject>>(jsonData);
When rrying to convert this into json object with Netwonsoft.json ,I am get getting error "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Model.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."
I am using the following classes
public class RootObject
{
public List<Entry> entry;
}
public class Entry
{
public string uid;
public string id { get; set; }
public int time { get; set; }
public List<string> changed_fields { get; set; }
}
Can some please tell where am i getting it wrong ?
Your JSON doesn't contain any RootObject
objects - it just contains strings. You've got an array of arrays of strings, where each "nested" array just contains a single string, and each string is the JSON representation of a RootObject
.
If you can possibly change whatever's producing this, that would be useful. Otherwise, you could use something like (untested):
JArray array = JArray.Parse(json);
List<RootObject> roots =
array.Cast<JArray>()
.Select(innerArray => (string) innerArray[0])
.Select(text => JsonConvert.DeserializeObject<RootObject>(text))
.ToList();
I've used two Select
calls for clarity - basically one extracts the single string from each nested array, and the other converts it to a RootObject
.
See more on this question at Stackoverflow