Deserializing oData to a sane object with ServiceStack -


so here's i'm getting odata service...

{      "odata.metadata":"http://server.ca/mediasite/api/v1/$metadata#userprofiles",    "value":[         {            "odata.id":"http://server.ca/mediasite/api/v1/userprofiles('111111111111111')",          "quotapolicy@odata.navigationlinkurl":"http://server.ca/mediasite/api/v1/userprofiles('111111111111111')/quotapolicy",          "#setquotapolicyfromlevel":{               "target":"http://server.ca/mediasite/api/v1/userprofiles('111111111111111')/setquotapolicyfromlevel"          },          "id":"111111111111111",          "username":"testuser",          "displayname":"testuser large",          "email":"testuser@testuser.ca",          "activated":true,          "homefolderid":"312dcf4890df4b129e248a0c9a57869714",          "moderatoremail":"testuser@testuserlarge.ca",          "moderatoremailoptout":false,          "disablepresentationcontentcompleteemails":false,          "disablepresentationcontentfailedemails":false,          "disablepresentationchangeowneremails":false,          "timezone":26,          "presenterfirstname":null,          "presentermiddlename":null,          "presenterlastname":null,          "presenteremail":null,          "presenterprefix":null,          "presentersuffix":null,          "presenteradditionalinfo":null,          "presenterbio":null,          "trustdirectoryentry":null       }    ] } 

i want deserialize simple class, important stuff (id, username, etc...to end).

i have class create, life of me can't figureout how throw away wrapper objects odata puts around thing.

can shed light?

you can use jsonobject dynamically traverse json, e.g:

var users = jsonobject.parse(json).arrayobjects("value")     .map(x => new user     {         id = x.get<long>("id"),         username = x["username"],         displayname = x["displayname"],         email = x["email"],         activated = x.get<bool>("activated"),     });  users.printdump(); 

or deserialize model matches shape of json, e.g:

public class odatauser {     public list<user> value { get; set; } }  public class user {     public long id { get; set; }     public string username { get; set; }     public string displayname { get; set; }     public string email { get; set; }     public bool activated { get; set; }     public string homefolderid { get; set; }     public string moderatoremail { get; set; }     public bool moderatoremailoptout { get; set; }     public bool disablepresentationcontentcompleteemails { get; set; }     public bool disablepresentationcontentfailedemails { get; set; }     public bool disablepresentationchangeowneremails { get; set; }     public int timezone { get; set; } }  var odata = json.fromjson<odatauser>(); var user = odata.value[0]; 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -