3

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.

dbc
  • 80,875
  • 15
  • 141
  • 235
mumu2
  • 611
  • 1
  • 5
  • 15
  • JSON Arrays are not restricted to one type (So the json is valid), in fact in javascript those objects would be all considered as different types in a sense because they are just generic object literals. – Esailija Nov 22 '12 at 14:54
  • I've edited the question title to be more correct. – mumu2 Nov 22 '12 at 14:56
  • Did you find an answer? Or you ended up not using DataContract – F Yaqoob Jul 03 '14 at 21:20
  • 1
    @FYaqoob, i gave up using DataContractJsonSerializer and switched to custom converter solution with Json.NET – mumu2 Jul 04 '14 at 11:59

1 Answers1

1

I usually use the JavascriptSerializer class instead:

JavaScriptSerializer js = new JavaScriptSerializer();
Object obj = js.DeserializeObject(str);
Response.Write(obj["photos"][0]); // 19
Asad Saeeduddin
  • 43,250
  • 5
  • 81
  • 127