Saturday 15 June 2013

c# - Newtonsoft.json parsing anonymous objects -



c# - Newtonsoft.json parsing anonymous objects -

i'm having problem figuring out how parse json object anonymous objects. here json:

{ "success": true, "affectedrows": 2, "data": [ { "success": true, "affectedrows": 1, "data": [ { "id": 376, "someotherid": 0, "stringvalue": "fan" } ] }, { "success": true, "affectedrows": 1, "data": [] }, { "success": true, "data": { "id": 401, "datetime": "2014-10-03 18:52:48" } } ] }

i have class contains classes work models json response i'm expecting.

public class jsonobjects { public class success { public bool success {get;set} public int affectedrows {get;set;} } public class response { public int id {get;set;} public int someotherid {get;set;} public string stringvalue {get;set;} } public class firstresponse : success { public response[] info {get;set;} } public class secondresponse : success { public object[] info {get;set;} } public class timedata { public int id {get;set;} public string datetime {get;set;} } public class fulltimedata { public bool success {get;set;} public timedata info {get;set;} } public class fullresponse : success { // ??? anonymous object[] info ??? } }

usage:

fullresponse info = jsonconvert.deserializeobject<jsonobjects.fullresponse>(jsonstring, jsonserializersettings); debug.writeline(data.data[0].anonymousobject0.data[0].stringvalue);

how can place anonymous objects in fullresponse class array of objects , later access them?

mmm, that's toughie. first off, way can utilize anonymous types outside local context in created utilize dynamic. possible deserialize json wholly dynamic types, newtonsoft.json won't out of box; more info can found here. however, avoid using dynamic @ cost, bypasses powerful tool c# developer has producing right code; compiler syntax checks. fat-finger identifier, or utilize square brackets instead of parenthesis on method call, , long could valid c# syntax, compiler no longer care whether is valid, leaving find out @ runtime.

staying in static land, "data" field going biggest problem in deserializing this, because it's array type in 1 instance, it's struct type. without discriminator of kind, or one-to-one relationship between field name , field type, can't think of way deserialize construction strongly-typed graph as-is.

let's "data" array; alter in json set of square brackets around actual info struct lastly object:

{ "success": true, "affectedrows": 2, "data": [ { "success": true, "affectedrows": 1, "data": [ { "id": 376, "someotherid": 0, "stringvalue": "fan" } ] }, { "success": true, "affectedrows": 1, "data": [] }, { "success": true, "data": [ { "id": 401, "datetime": "2014-10-03 18:52:48" } ] } ] }

in case, define single, self-nesting type has property every info field find @ level of graph:

public class rawresponse { public bool? success {get;set} public int? affectedrows {get;set;} public int? id {get;set;} public int? someotherid {get;set;} public string stringvalue {get;set;} public string datetime {get;set;} public rawresponse[] info {get;set;} }

this allow deserialize json statically-typed object graph. then, can add together method produces particular derived implementation of response, based on fields set:

public class rawresponse : response { public bool? success {get;set} public int? affectedrows {get;set;} public int? id {get;set;} public int? someotherid {get;set;} public string stringvalue {get;set;} public string datetime {get;set;} public rawresponse[] info {get;set;} public response toresponse() { if(id.hasvalue && someotherid.hasvalue && stringvalue.) homecoming new otheridresponse{ id = id, someotherid = someotherid, stringvalue = stringvalue }; if(id.hasvalue && datetime.hasvalue) homecoming new datetimeresponse{id = id, datetime = datetime}; //default case; success , kid info optional affectedrows homecoming new compoundresponse{ success = success, affectedrows = affectedrows, // can null info = data.select(d=>d.toresponse()) .toarray() }; } }

obviously you'll need objects similar ones have, derived mutual "response".

the lastly hurdle knowing specific type of given element. that, recommend "discriminator"; mutual property providing easy, unique type identifier:

public abstract class response { public string reponsetypename{ { homecoming gettype().name; } } }

c# json json.net

No comments:

Post a Comment