I have been searching for days, hours at a time trying to find an answer to my question. I have the following JSON string:
{
"id": "658@787.000a35000122",
"take": [{
"level": [0],
"status": [[3, [0]]]
}]
}
That is a sample of a variety of messages that I need to deserialize, but it is the one that is causing me heartache right now. The problematic portion for me is the "status" array. My class to accept the results of deserializing the string is:
[DataContract]
public class ReceivedMsg
{
public ReceivedMsg()
{
move = new List<MoveOperation>();
}
[DataMember]
public string id { get; set; }
[DataMember]
public List<MoveOperation> move { get; set; }
[DataContract]
public class Status
{
[DataMember]
public int destination { get; set; }
[DataMember]
public int[] source { get; set; }
}
[DataContract]
public class MoveOperation
{
public MoveOperation()
{
status = new List<Status>();
}
[DataMember]
public int[] level;
[DataMember]
public List<Status> status { get; set; }
}
}
The code to do the deserializing is:
ReceivedMsg m = new ReceivedMsg();
m = JsonConvert.DeserializeObject<ReceivedMsg>(strResp, new JsonSerializerSettings { TraceWriter = traceWriter });
Where strResp is the string containg the JSON data.
I initially tried using the JSON library that is a part of the .NET framework, but got stuck on the "status" portion. That's what prompted me to try Json.NET.
The error I am getting is:
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Roper.Roper+ReceivedMsg+Status' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> 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 'move[0].status[0]', line 6, position 16.
Any help would be greatly appreciated. Of course I will be happy to furnish additional information as needed. I tried doing a custom converter, but I think my C# knowledge is not quite at that level yet. I have been trying to decipher the solutions offered in answer to similar questions, but concluded that I must be missing something.
My sincere thanks to the community for taking the time to read my lengthy question. Your generosity continues to amaze me!