S
S
Sergei Cojocaru2013-09-09 20:34:23
JSON
Sergei Cojocaru, 2013-09-09 20:34:23

json to c# class

Guys, in short, this JSON caught my eye. I don't even know how to represent it as a class in C#

[
"is a good for child?",
[
[
" what is a good pet for a child",
0,
[
8,
30
]
],
[
" how long is a child 's passport good for",
0,
[
8,
30
]
],
[
" what is a good first pet for a child",
0,
[
8,
30
]
],
[
"is a rabbit a good pet for a child",
0,
[
8,
30
]
]
],
{
"t": {
"bpc": false,
"tlw": false
},
"q": "ul2-5osqijK9Cm5xhyiRfRuPxEk "",
"j": "g"
}
]

Answer the question

In order to leave comments, you need to log in

5 answer(s)
F
FanKiLL, 2013-09-09
@FanKiLL

Not valid json. In general, do a copy / paste here, the service will give you the models you need.

M
mayorovp, 2013-09-09
@mayorovp

Apparently, this is an object[]

N
Nikolai Turnaviotov, 2013-09-09
@foxmuldercp

in this form, it is not very readable, to be honest, it’s better to have a piece of code or format it as code or somewhere on paste.org.ru/etc…

P
petuhov_k, 2013-10-03
@petuhov_k

Perhaps I misunderstood the question, but if using Newton is not important, then your json is easily deserialized like this:
var result = new JavaScriptSerializer().DeserializeObject(jsonString);
Get an object of type object[], where
object[0] is the string “is a good for child?”
object[1] - array of arrays of strings/numbers
object[2] - dictionary for :{"t": {{"bpc": false,"tlw": false},"q": "ul2-5osqijK9Cm5xhyiRfRuPxEk","j ': 'g'}
Using the JavaScriptSerializer class from System.Web.Extensions

M
Mykola Dzedzinskyi, 2014-02-09
@dzedzinskiy

You should have something like these classes:

public class InnerObject
{
     public string InnerQuestion{get;set;}
     public int Number{get;set;}
     public List<int> Numbers{get;set;}
}
public class OuterObject // этот класс - представление json объекта
{
     public string OuterQuestion{get;set;}
     public List<InnerObject> ListOfObjects{get;set;}
     public Dictionry<string, object> Dict{get;set;} // тут не уверен т.к. не знаю возможно ли повторение ключей, в более общем случае используйте Dictionry<object, object>
}

//в исполняемой среде :
var jss = new JavaScriptSerializer();
OuterObject object = jss.Deserialize<OuterObject>(json_data);

But here you need to play around with the names of the class properties so that everything beautifully deserializes itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question