I have data of the following form:
{
"sections" : [
{
"section" : {
"Term" : "News",
"Term ID" : "4,253"
}
},
{
"section" : {
"Term" : "Sports",
"Term ID" : "4,254"
}
},
// ...
]
}
I would like to serialize it into a collection of the following class:
public class Section
{
public string Name;
public int Tid;
}
Here is the code I'm using to do it, using JSON.NET:
// e.Result is the downloaded JSON
JObject jsonData = JObject.Parse(e.Result);
var sections = jsonData["sections"].Select(obj => obj["section"]).Select(sectData => new Section()
{
Name = HttpUtility.HtmlDecode(sectData["Term"].Value<string>().Replace("\"", "")),
Tid = int.Parse(sectData["Term ID"].Value<string>().Replace(",", ""))
});
foreach (Section s in sections)
{
// _sections is an ObservableCollection<Section>
_sections.Add(s);
}
It feels a bit clunky. Can I do this more elegantly?
Particularly that foreach
loop at the end. I'd rather use a method like addAll
or concat
or something.