I am trying to parse some JSON on the Windows Phone with DataContractJsonSerializer. All of the JSON responses have the same format: status, message, data. The status and message field always have the same type, but the data field contains a call-specific object. Here are some examples:
Hash
{
status: 0,
message: "No error",
data:
{
team: "test",
startTime: "1969-12-31 19:00:00 -0500",
endTime: "2000-01-01 00:00:00 -0500",
max_photos: 30,
max_judged_photos: 24
}
}
List
{
status: 0,
message: "No error",
data:
[
{
id: 1,
game_id: "Test",
description: "Test",
points: 100
},
{
id: 2,
game_id: "Test",
description: "Test",
points: 1000
}
]
}
I would like to parse the responses into a generic Response object containing status, message, and data. I want to then further parse the JSON in data into the correct object (hash or list). Here is what my Response class currently looks like:
[DataContract]
public class Response
{
[DataMember(Name = "status", IsRequired=true)]
public STATUS Status { get; set; }
[DataMember(Name = "message", IsRequired = true)]
public string Message { get; set; }
[DataMember(Name = "data", IsRequired = true)]
public ?????? Data { get; set; }
}
My question is, is there a generic JSON container I can use to hold the inner JSON in the data field (so that I can parse that seperately) or is there a better way of solving this altogether? I’d rather not have separate Response classes for each response type.
Thank you.
Since
datais sometimes an array and sometimes a single object, you can not deserialize it to a type safe class.But you can use Json.Net (which works on WP7) to parse the json string.