-1

So i have a json file where one of the object name is the id of the person. So for each person its a different id. So i dont know how to serialize it.

{"19887289":[{"name":"Twisted Fate's Dragons","tier":"MASTER","queue":"RANKED_SOLO_5x5","entries":[{"playerOrTeamId":"19887289","playerOrTeamName":"Imaqtpie","division":"I","leaguePoints":237,"wins":72,"losses":37,"isHotStreak":false,"isVeteran":false,"isFreshBlood":true,"isInactive":false}]}]}

This is my json output.

And when i go to http://json2csharp.com/ and input it. It gives me a class name with __invalid_type__19887289

And this is what i have currently Dictionary< string, RootObject2 > root = JsonConvert.DeserializeObject< Dictionary< string, RootObject2 >>(jsonrank);

Edit ::: Fixed the issue by using this : Deserialize JSON into C# dynamic object?

Still curious how to solve my problem tho.

Community
  • 1
  • 1
John Doe
  • 39
  • 6
  • What IS `Rootobject2`? Show its definition – MakePeaceGreatAgain Mar 12 '16 at 23:38
  • @HimBromBeere Its just the main object. All it contains is a list with a object that is my json output. You can get a better answer by going to json2csharp.com and pasitng my json output into it – John Doe Mar 12 '16 at 23:41
  • `Dictionary< string, RootObject2 >` is the best solution, since your JSON is basically a dictionary of objects keyed by an ID. I suppose you could use `Dictionary< long, RootObject2 >` if you are sure the id is always numeric. Or perhaps something more descriptive than `RootObject2`, maybe `GameObject` or whatever. – dbc Mar 13 '16 at 00:01

2 Answers2

1

All you need is to deserialize to a dictionary

var players = JsonConvert.DeserializeObject<Dictionary<string, Player>>(json);



public class Entry
{
    public string playerOrTeamId { get; set; }
    public string playerOrTeamName { get; set; }
    public string division { get; set; }
    public int leaguePoints { get; set; }
    public int wins { get; set; }
    public int losses { get; set; }
    public bool isHotStreak { get; set; }
    public bool isVeteran { get; set; }
    public bool isFreshBlood { get; set; }
    public bool isInactive { get; set; }
}

public class Player
{
    public string name { get; set; }
    public string tier { get; set; }
    public string queue { get; set; }
    public List<Entry> entries { get; set; }
}
Eser
  • 12,346
  • 1
  • 22
  • 32
-1

You can override the JSON property name of your model when being serialized or deserialized using Newtonsoft.Json

[JsonProperty("integer")]
public string Number {get;set;}

When you serialize your model, instead of "Number" you'll see "integer" as property name. Also, if you want to deserialize a JSON string, the "integer" property will be your "Number".

  • I might be explain this wrong. The object name is a variabele. So for each request it changes. So i cant really create objects for them because they dont know have name only a value that is constantly different. – John Doe Mar 13 '16 at 00:02