Parsing dynamic JSON string into string in C# using JSON.NET -


this first little project on c# , json. i'm asked parse json file following: i'm trying create windows form body contain content of json string in following format:

name of object (label) name of attribute of object - (textbox) editable value (label) name of attribute of object - (textbox) editable value ... 

i have approx 35 attributes per object in json file , 8 objects. there around 50 different attributes in total. have searched json - c# - json.net , read more 15 questions & answers. answers had valuable information starters me, not associate answers situation. these reasons:

  • some answers converted json string c# objects. since need "names" of attributes of objects, option did not strike me solution. have no idea how name of variable in source code , use in windows form.
  • some answers converted json string <dictionary> data structure. i'm not aware of dictionaries , properties, definition think, dictionaries map 2 values each other. in case, 3-5 different objects may have same attribute, therefore 1 attribute there multiple values.

besides that, example i've seen was:

var dict = new javascriptserializer().deserialize<dictionary<string,object>>(json); var postalcode = dict["postalcode"];

since have around 50 attributes, using approach not option me: using field names 1 one done in example ("postalcode" 1 of attributes in example json object in referenced question).

at first glance, thought might parse on own using string manipulation. want use beautiful json.net library. i'm sort of stuck here, having no idea how name of attribute of json object , use , value in windows form. have ideas in mind have no idea how implement them.

i might parse json string contains 8 objects object array, each object containing 2d string arrays attribute name , value. convert string float when editing value. need them strings because want use them in windows form. solution in mind. how can proceed?

since have dynamic json string, i'd recommend parsing string jobject. gives ultimate flexibility far processing json object tree:

var parsetree = jsonconvert.deserializeobject<jobject>("{ a: 2, b: \"a string\", c: 1.75 }"); foreach (var prop in parsetree.properties()) {     console.writeline(prop.name + ": " + prop.value.toobject<object>()); } 

another jobject example:

var parsed = jsonconvert.deserializeobject<jobject>("{\"name\":{\"a\":2, \"b\":\"a string\", \"c\":3 } }"); foreach (var property in parsed.properties()) {     console.writeline(property.name);     foreach (var innerproperty in ((jobject)property.value).properties()) {         console.writeline("\t{0}: {1}", innerproperty.name, innerproperty.value.toobject<object>());     } } 

if know dealing json array, can instead parse jarray , @ each jobject in array:

var properties = jsonconvert.deserializeobject<jarray>("[{a:1}, {b:2, c:3 }]")     .selectmany(o => ((jobject)o).properties())     .toarray(); foreach (var prop in properties) {     console.writeline(prop.name + ": " + prop.value.toobject<object>()); } 

for more flexibility, can parse jtoken.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -