mumu2
mumu2

Reputation: 671

Deserialize json with array of different types using DataContractJsonSerializer

Is there a way to deserialize such json

{
  "photos": [
    19,
    {
      "pid": 288777129,
      "aid": -6
    },
    {
      "pid": 286441792,
      "aid": -6
    },
    {
      "pid": 114893258,
      "aid": 34465839
    }
  ]
}

with C# DataContractJsonSerializer without modifying the source. I mean the first element 19.

Upvotes: 3

Views: 670

Answers (1)

user1726343
user1726343

Reputation:

I usually use the JavascriptSerializer class instead:

JavaScriptSerializer js = new JavaScriptSerializer();
Object obj = js.DeserializeObject(str);
Response.Write(obj["photos"][0]); // 19

Upvotes: 1

Related Questions