Home > Net >  About the list <T> JSON deserialization question! Help bosses!
About the list <T> JSON deserialization question! Help bosses!

Time:03-12

Self-study just to fit in c # write the copy with the JSON data to the LIST of code, but there is now a doubt is, if I am the number of the LIST elements in the LIST is not fixed, how to define?




 
Public struct ToJsonMy
{
Public string err_msg {get; set; }
Public int ret_code {get; set; }
Public List Lasermsgs;

}
Public struct lasermsg
{
Public List Beams;
Public string device_info;

Public struct beamsStruct
{
Public int Angle {get; set; }
Public int dist {get; set; }
Public bool is_obstacle {get; set; }


}

}
String json="[{" err_msg \ ": " error! \ ", \ "ret_code " : \ "2 ", \ "lasermsgs " : [{\ "device_info " : \ "aaaaaaa! ", \ "beams " : [{\ "Angle " : \ "20 ", \ "dist " : \ "5 ", \ "is_obstacle " : \ \ "false"}]}]}] ".

Javascriptserializer js=new javascriptserializer ();//instantiate a class can be serialized data
Var list=js. Deserialize (json);//convert the json data into object types and assign a value to a list
Int ret_code=list [0]. Ret_code;
String err_msg=list [0]. Err_msg;

List Beamsstructs=list [0]. Lasermsgs [0]. Beams;

The console. Readkey ();


Just as I return to the list of the json data as follows:
{
"Crisis" : [
{
"Id" : "LM1,"
"Type" : "LocationMark,"
"X" : 1.23,
"Y" : 4.56,
"R" : 1.57,
"Desc" : "XXXXX"
},
{
"Id" : "LM2,"
"Type" : "LocationMark,"
"X" : 1.23,
"Y" : 4.56,
"R" : 3.14,
"Desc", "
"},
{
"Id", "" CP1,
"Type" : "ChargePoint",
"X" : 1.23,
"Y" : 4.56,
"R" : 0,
"Desc", "
"}
],
}
"Crisis" is the number of the list is not fixed, don't know how many sites, sincerely for advice! Thank you very much!

CodePudding user response:

The definition of station class?
You have to do is station object List, its length is not fixed, need not what ah, to understand the basic data structure, the realization of the List is a List, is long, is not an array,

CodePudding user response:

List<> Originally is not a fixed number of

Deserialization, not need you to build an object him populate data
But return a serialized objects to you

In fact, you are deserialized into an array, is can
Js. Deserialize CodePudding user response:

List<> Is the data,
Javascriptserializer is a tool,
Data, no matter how much to tool is still the same work pattern,

Unless you List takes up a lot of memory, or there is a nested loop reference, etc.,
  •  Tags:  
  • C#
  • Related