Deserialize complex JSON data in C# -


i'm amateur in c# programming. have json data looks following

{   type: "xxx",   width: "xxx",   datasource: {       "chart": {           "caption": "xxx"        },       "data": [        {},        {}        ]   }  } 

i'm having whole data escaped string. after unescape when i'm using javascriptserializer follows

var data = ser.deserialize<dictionary<string, object>>(chartdata); 

i'm able "type", "width" as

data["width"] data["type"] 

now have value of "caption". suggestion how that, believe dictionary structure need changed i'm stacked lack of knowledge in c#

if know object's scheme man want create class represents in , deserialize json it:

yourknownclass obj = jsonconvert.deserializeobject<yourknownclass>(json); console.writeline(obj.datasource.chart.caption.value); 

another option using dynamic type (there no reason using dynamic object if know schema , can create matching c# class. has performance impact well):

dynamic obj = jsonconvert.deserializeobject<dynamic>(json); console.writeline(obj.datasource.chart.caption.value); 

btw, in example i'm using json.net popular library.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -