I am unfortunately forced to deal with a raft of JSON data and so inevitably defaulted to json.net, wherein I discovered the 'documentation' deals exclusively in fancifully simplistic examples.
My data contains the following 'instance', as an example.
"team" : [{
"id" : "8",
"vendor-id" : "8",
"statsinc-id" : "8",
"team-name" : "",
"team-nickname" : "Pistons",
"rank" : ""
}, {
"id" : "51",
"vendor-id" : "29",
"statsinc-id" : "29",
"team-name" : "",
"team-nickname" : "Grizzlies",
"rank" : ""
}
]
Now bear in mind either side of this string/whatever the json term is, is a large amount of other data.
There are three problems.
I followed the documentation and couldn't actually get it to spit out any kind of value (I was trying to get the id field).
The 'attributes' have hyphens in them, c# doesn't allow hyphens in variable/property names so how do you deal with that?
In the above instance there are two of each 'attribute' so presumably the latter would overwrite the former in the object, would I need a list of objects and then somehow figure out a way to insert each value sequentially?
I find the whole thing needlessly complicated and am therefore in dire need of help - it's a lot to ask but if someone could show me how to get both the 'team-nickname' values out I'd be massively grateful.
Thanks in advance.
For the name issue, you should use the JsonPropertyAttribute to specify what the property names are (when they can't be inferred, or you want it different).
There are multiple items, but they're in an array so that you can access both. They do not overwrite each other.
You can use JSONLint to validate JSON and json2csharp to try to generate C# classes based on JSON.
Your JSON, as-is, is not valid full JSON. I wrapped it in {} to make it an object, and then was able to create these classes:
public class RootObject
{
public List<Team> team { get; set; }
}
public class Team
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("vendor-id")]
public string VendorId { get; set; }
[JsonProperty("statsinc-id")]
public string StatsIncId { get; set; }
[JsonProperty("team-name")]
public string TeamName { get; set; }
[JsonProperty("team-nickname")]
public string TeamNickname { get; set; }
[JsonProperty("rank")]
public string Rank { get; set; }
}
// this works!
var obj = JsonConvert.DeserializeObject<RootObject>(data);
var nicknames = obj.team.Select(x => x.TeamNickname); // "Pistons", "Grizzlies"
I used JsonProperty to specify all of the property names in Team (not just the hyphenated ones), so that I can use the C#/.Net standard property capitalization while still serializing and deserializing correctly.
Create a class for your team object:
public class Team {
[JsonProperty(PropertyName = "id")]
public int ID {get;set;}
[JsonProperty(PropertyName = "vendor-id")]
public int VendorID {get;set;}
[JsonProperty(PropertyName = "statsinc-id")]
public int StatsIncID {get;set;}
[JsonProperty(PropertyName = "team-name")]
public string TeamName {get;set;}
[JsonProperty(PropertyName = "team-nickname")]
public string TeamNickname {get;set;}
[JsonProperty(PropertyName = "rank")]
public Rank {get;set;}
}
Then, you can deserialize your JSON by doing:
var listOfTeams = JsonConvert.DeserializeObject<List<Team>(inputString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With