Best way to handle .NET json [1,2, "three"]

-edit- Maybe there is a solution using attributes?

I found the weirdest json. It looks like the line below. The only way I can handle it is to use an array of objects. I have never seen ints and strings mix in the array below.

Then it forces me to type. Is there a better way to get int and string?

NOTE. The first array can contain from 0 to 20 elements. Inside the ALWAYS array, there are 3, with the first two being ints and the third being a string. Maybe I can use this knowledge to my advantage?

{
 var ser = new JavaScriptSerializer();
 var o = ser.Deserialize<RootObj>(@"{""resources"": [[123,567,""<div ...""]]}");
 var o2 = o.resources[0];
 var v = (int)o2[1];
 var sz = (string)o2[2];
}
class RootObj {
 public object[][] resources;
}
0
source share
2 answers

Ignore it because using the object is not bad.

0

, LINQ . o, - .

var result = o.Select(c => new
                      {
                          v1 = (int)c[0],
                          v2 = (int)c[1],
                          sz = (string)c[2],
                      });

, new { ... } , , , .

, , ... , .

0

Source: https://habr.com/ru/post/1758909/


All Articles