How to describe the structure of classes for parsing such a json-string coming from VKontakte servers using Json.Net?

{ "ts":1820350874, "updates":[  [ 4, 1619489, 561, 123456, 1464958914, " ... ", "hello", { "attach1_type":"photo", "attach1":"123456_414233177", "attach2_type":"audio", "attach2":"123456_456239018" } ] ] } 

I tried to make it through a special insert in Visual Studio - I swear that there is an incorrect Json.

Convert everything to object'y, and only then convert?

Last time I decided that the updates array was described as List>, and when I needed to get the value I converted it to a specific type.

  • 3
    json-generator.com - use the site. You have a problem with json - Misha
  • one
    Where do you get invalid json from? - Ev_Hyper
  • This json comes from the longpoll server vkontakte. they always have everything in one place, so you have to invent something yourself. And in the documentation it is used in the example of the answer - Arthur

2 answers 2

in Json you need to give the names of all fields, for example like this:

 { "ts": "1820350874", "updates": { "number": 4, "number2": 1619489, "number3" : 561, "number4" : 123456, "number5": 1464958914, "dot": " ... ", "hello":"hello", "obj": { "attach1_type": "photo", "attach1": "123456_414233177", "attach2_type": "audio", "attach2": "123456_456239018" } } } 

UPDATE

To deserialize Json with unknown fields, try using this:

 var serializer = new JavaScriptSerializer(); var result = serializer.DeserializeObject(json); 

I think it should solve the problems.

  • I know this, but the fact is that the number and name of these fields is unknown to me, because they have no names in principle. Just a set of elements in an array. Last time I decided by creating a List <List <object >> and parsing my json into it - Arthur
  • updated the answer for parsing unknown data in advance - Nikolay
  • Yes, I just installed Json.Net, I wanted to do everything through it. So I could have done everything through the UWP's JSON parser. Anyway - thanks, it helped - Arthur

dynamic JSON = JObject.Parse (string js);

Then you can work with JSON like this: JSON ["ts"] ["updates"]

This method is better and easier.