.net - C# JSON.NET - Deserialize response that uses an unusual data structure -


i having trouble figuring out clean (as possible) way deserialize json data in particular format. want deserialize data typed data object classes, pretty flexible regarding specifics of this. here example of data looks like:

{     "timestamp": 1473730993,     "total_players": 945,     "max_score": 8961474,     "players": {             "player1username": [             121,             "somestring",             679900,             5,             4497,             "anotherstring",             "thirdstring",             "fourthstring",             123,             22,             "yetanotherstring"],         "player2username": [             886,             "stillastring",             1677,             1,             9876,             "alwaysastring",             "thirdstring",             "fourthstring",             876,             77,             "string"]         } } 

the specific parts unsure are:

  1. would collection of players considered dictionary? username serve key, value throwing me off since mixed collection of string , integer values.
  2. a player comprised entirely of unnamed values. have pretty worked json data had named properties , values (ex. timestamp, total_players, etc. @ top)

say have top level class this:

public class scoreboardresults {     public int timestamp { get; set; }     public int total_players { get; set; }     public int max_score { get; set; }     public list<player> players { get; set; } } 

what player object given key/value username serving key, , value being collection of mixed integers , strings? data each player element in same order, know first value in collection uniqueid, second value player description, etc. player class this:

public class player {     public string username { get; set; }     public int uniqueid { get; set; }     public string playerdescription { get; set; }     ....     ....     .... following pattern of values in each player element     ....     .... } 

i sure pretty straightforward thing using json.net, why wanted avoid of ideas had on how accomplish this. came have been un-elegant , error prone degree during serialization process.

edit

here classes generated when using past json classes suggested snow_ffffff:

public class rootobject {     public int timestamp { get; set; }     public int total_players { get; set; }     public int max_score { get; set; }     public players players { get; set; } }  public class players {     public object[] player1username { get; set; }     public object[] player2username { get; set; } } 

what not clear me how deserialize json data in "players" element list player1username being simple string property on player object. collection of intermixed strings , integers, confident can individual properties on player object without issue.

the converter deserializing json in visual basic .net should need, suitably translated vb.net c#:

public class objecttoarrayconverter<t> : jsonconverter {     public override bool canconvert(type objecttype)     {         return typeof(t) == objecttype;     }      static bool shouldskip(jsonproperty property)     {         return property.ignored || !property.readable || !property.writable;     }      public override void writejson(jsonwriter writer, object value, jsonserializer serializer)     {         var type = value.gettype();         var contract = serializer.contractresolver.resolvecontract(type) jsonobjectcontract;         if (contract == null)             throw new jsonserializationexception("invalid type " + type.fullname);         var list = contract.properties.where(p => !shouldskip(p)).select(p => p.valueprovider.getvalue(value));         serializer.serialize(writer, list);     }      public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)     {         if (reader.tokentype == jsontoken.null)             return null;         var token = jtoken.load(reader);         if (token.type != jtokentype.array)             throw new jsonserializationexception("token not array");         var contract = serializer.contractresolver.resolvecontract(objecttype) jsonobjectcontract;         if (contract == null)             throw new jsonserializationexception("invalid type " + objecttype.fullname);         var value = existingvalue ?? contract.defaultcreator();         foreach (var pair in contract.properties.where(p => !shouldskip(p)).zip(token, (p, v) => new { value = v, property = p }))         {             var propertyvalue = pair.value.toobject(pair.property.propertytype, serializer);             pair.property.valueprovider.setvalue(value, propertyvalue);         }         return value;     } } 

next, add converter player class, , indicate order of each property using jsonpropertyattribute.order:

[jsonconverter(typeof(objecttoarrayconverter<player>))] public class player {     [jsonproperty(order = 1)]     public int uniqueid { get; set; }     [jsonproperty(order = 2)]     public string playerdescription { get; set; }     // other fields required. } 

then finally, declare root object follows:

public class scoreboardresults {     public int timestamp { get; set; }     public int total_players { get; set; }     public int max_score { get; set; }     public dictionary<string, player> players { get; set; } } 

note have moved username out of player class , dictionary, key.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -