Saturday 23 November 2019

c# - Deserialize JSON to?




I have JSON which I need to deserialize, but I don't want to create class with property name.




here's what I get in JSON:



"[{"id":1,"width":100,"sortable":true}, {"id":"Change","width":100,"sortable":true}]"


So how could I do this?



Thanks for advance:)


Answer




You can use JavaScriptSerializer



var list = new JavaScriptSerializer()
.Deserialize>>(json);

var id = list[0]["id"];


Or if you want, Json.Net




var list2 = JsonConvert.DeserializeObject>>(json);


Json.Net also allows you to use dynamic



dynamic list = JsonConvert.DeserializeObject(json);
var wdth = list[0].width;

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...