more json c# issues -
this continuation of previous question of mine. solution worked trying @ time broke whole lot of code. know bad practice not make sure beforehand yeah... live , learn.
anyhoo, here's q: an easy way serialise c# objects
what want know is: there way newtonsoft library handle stuff? if yes, how? if no, suggestions?
what i'm doing chatting ror3 app using json, cant deserialise response. here's little code:
the response i'm getting ror looks like:
[{"directory":{"created_at":"2011-07-20t22:29:38z","drive_info":1,"id":15,"name":"new drive","parent":0,"size":0,"updated_at":"2011-07-20t22:29:39z","user":1}}]
i'm trying deserialise list of directory objects using:
public static compositecollection deserialise<t>(string json) { compositecollection result = new compositecollection(); jarray arr = jarray.parse(json); foreach (jobject obj in arr) { result.add(jsonconvert.deserializeobject<t>(obj.first.first.tostring())); } return result; }
and relevant part of directory class looks like:
// [serializable] // [datacontract] public class directory { // [datamember] public int id { get; set; } // [datamember] public string name { get; set; } // [datamember] public int parent { get; set; } // [datamember] public int drive_info { get; set; } // [datamember] public int size { get; set; } // [datamember] public int user { get; set; } // [datamember] public string state { { /* if (parent == 0) return _state.identify(); directory parental; return parental.state;*/ if (parental != null) return parental.state; return _state.identify(); } set { _state = statefactory.getstate(value); } } //[jsonignore] blah...
i can deserialise of time uncommenting [serializable] (sometimes following error: object of type 'system.int32' cannot converted type 'offloadui.directory'. still investigating), , can serialise uncomenting [datacontract] , instances of [datamember]. need work in both directions.
thanks zootius found useful guide. here's did, works bought one:
[jsonobject(memberserialization.optin)] public class directory { [jsonproperty] public int id { get; set; } [jsonproperty] public string name { get; set; } [jsonproperty] public int parent { get; set; }
Comments
Post a Comment