Reputation: 7444
Is it possible to deserialize this json using JSON.NET?
"players": {
"0": {
"success": 1,
"name": "xsusususdd"
},
"1": {
"success": 1,
"name": "bleeps"
},
..."n": {
"success": 1,
"name": "bloops"
}
}
The 3rd party web service that I'm using doesn't return an array but rather an object that is made up of an arbitrary number of nested objects.
I'm starting with something along the lines of:
public class Players
{
public Player 0 {get;set;} //cant name the Player 0
public Player 1 {get;set;} //cant name the Player 1
public List<Players> players {get;set;} //doesn't work because it isn't being returned as an array
}
public class Player
{
public string success { get; set; }
public string name { get; set; }
}
var URL = new WebClient().DownloadString("http://webservice");
Players result = JsonConvert.DeserializeObject<Players>(URL);
Upvotes: 1
Views: 205
Reputation: 64
You could use this instead.
"players": [
{
"id": 0,
"success": 1,
"name": "xsusususdd"
},
{
"id": 1,
"success": 1,
"name": "bleeps"
},
{
"id": n,
"success": x,
"name": y
}
]
This will create a list with personobjects that you could turn into a dictionary
Dictionary<string, Player> dictionary = players.ToDictionary(v => v.id, v => v);
or just a List<Person>
or what you prefer.
If you need to access them from javascript just use e.g players[1].name
.
Upvotes: 3
Reputation: 50114
You should be able deserialize as a Dictionary<string, Player>
(or possibly <int, Player>
).
Once you have that, you can create a Players
class from your dictionary.
Upvotes: 3