简体   繁体   中英

C# deserialization

How to deserialize with keyvaluepair the above json

string stations = [{"2":false,"1":"Aforre","0":"WS6"},{"2":false,"1":"Alodtau","0":"WS3"},{"2":false,"1":"Balimo Station","0":"WS36"}]

I what like this

var get = js.Deserialize<Dictionary<string,dynamic>>(stations);

try this:

string stations = "[{'2':false,'1':'Aforre','0':'WS6'},{'2':false,'1':'Alodtau','0':'WS3'},{'2':false,'1':'Balimo Station','0':'WS36'}]";
var serializer = new JavaScriptSerializer();
dynamic value = serializer.DeserializeObject(stations);

and you can access objects like:

var a = value[0]["0"];

and a will have "WS6" (according to your JSON)

The JSON shown is an array. You might try desetializing to:

Dictionary<string, object>[]

ie

var get = js.Deserialize<Dictionary<string,object>[]>(stations);

最好的选择之一是创建一个由所有键值对组成的类,然后将json字符串反序列化为所创建类的对象

You can try using dynamic variable as following:

 string stations = "[{'2':false,'1':'Aforre','0':'WS6'},{'2':false,'1':'Alodtau','0':'WS3'},{'2':false,'1':'Balimo Station','0':'WS36'}]";
 var serializer = new JavaScriptSerializer();
 dynamic serializevalues = serializer.DeserializeObject(stations);
 var valueof1 = serializevalues[0]["1"];
 Response.Write(valueof1); 

The above will print output "Aforre'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM