Consuming JSON elements in C# that are not in an array -
i consuming json output 1 source contains array , 1 not.
the 1 array simple, can create class represents object , list of objects, iterate through list , properties each object. in source not have array, however, throwing me loop.
i not know how iterate through this. seems if need create separate classes "abc" , "def" though properties of each class same. there simple way this?
example not contain array:
{ "objectcontainer": { "count": 25, "objects": { "abc": { "name": "object1", "parent": "0", "status": "0", }, "def": { "name": "object2", "parent": "0", "status": "0", } etc....
thanks in advance assistance.
you use inheritance prevent repeating properties "abc" , "def" on , on again.
public class base { public string name { get; set; } public string parent { get; set; } public string status { get; set; } } public class abc : base { } public class def : base { } public class objects { public abc abc { get; set; } public def def { get; set; } } public class objectcontainer { public int count { get; set; } public objects objects { get; set; } } public class rootobject { public objectcontainer objectcontainer { get; set; } }
then using json.net can deserialize string.
var root = jsonconvert.deserializeobject<rootobject>( json );
the problem you're going have change code every time new object in there (e.g. ghi).
another option, particularly if you're going have different object names showing up, parse json serially yourself.
jsontextreader reader = new jsontextreader( new stringreader( json ) ); while( reader.read() ) { if( reader.value != null ) { console.writeline( "field: {0}, value: {1}", reader.tokentype, reader.value ); } }
obviously it's writing output console you'd have examine tokentype , value , stuff object.
update
this pretty ugly, curious how might parse object structure. you'd need change receiving object definitions bit.
public class base { public string name { get; set; } public string parent { get; set; } public string status { get; set; } } public class objects { public list<base> bases { get; set; } public objects() { bases = new list<base>(); } } public class objectcontainer { public int count { get; set; } public objects objects { get; set; } public objectcontainer() { objects = new objects(); } } public class rootobject { public objectcontainer objectcontainer { get; set; } public rootobject() { objectcontainer = new objectcontainer(); } }
then can parse using:
while( reader.read() ) { if( reader.value != null ) { switch( reader.depth ) { case 2: if( reader.tokentype == jsontoken.propertyname && reader.value.tostring() == "count" ) { reader.read(); root.objectcontainer.count = convert.toint32( reader.value ); } break; case 3: newbase = new base(); root.objectcontainer.objects.bases.add( newbase ); break; case 4: if( reader.tokentype == jsontoken.propertyname && reader.value.tostring() == "name" ) { reader.read(); newbase.name = reader.value.tostring(); } if( reader.tokentype == jsontoken.propertyname && reader.value.tostring() == "parent" ) { reader.read(); newbase.parent = reader.value.tostring(); } if( reader.tokentype == jsontoken.propertyname && reader.value.tostring() == "status" ) { reader.read(); newbase.status = reader.value.tostring(); } break; } } }
not prettiest code in world long structure of json doesn't change you'll end nice object model no matter how many child objects or names are.
Comments
Post a Comment