Reputation: 44075
I am using a Javascript library which expects data to be in a certain json format as show below. I will use .NET to get the data and feed it to the library using json deserialization. However I am not sure how C# classes are serialized to json. Will the built-in .NET json serializer support this json structure or do I have to invest in a more advanced library like json.net? I never used json.net. My approach is to do several trial and error approaches creating different C# classes and looking at the output json until the json structure matches the format below. This seems inefficient and time consuming and I wanted to know if there's a better approach.
Note: Each second level node can have 1 or more children. For example: the ones with playcount 276, 271.
{
"children": [
{
"children": [
{
"children": [],
"data": {
"playcount": "276",
"$color": "#8E7032",
"image": "http://userserve-ak.last.fm/serve/300x300/11403219.jpg",
"$area": 276
},
"id": "album-Thirteenth Step",
"name": "Thirteenth Step"
},
{
"children": [],
"data": {
"playcount": "271",
"$color": "#906E32",
"image": "http://userserve-ak.last.fm/serve/300x300/11393921.jpg",
"$area": 271
},
"id": "album-Mer De Noms",
"name": "Mer De Noms"
}
],
"data": {
"playcount": 547,
"$area": 547
},
"id": "artist_A Perfect Circle",
"name": "A Perfect Circle"
},
.....
Upvotes: 2
Views: 2433
Reputation: 977
Use this http://jsonclassgenerator.codeplex.com/
I have been using this for quite some time now and it is the best method of generating the C# class from your JSON. After you generate the class you can then serialize it using JSON.NET to get the desired JSON.
Upvotes: 0
Reputation: 65049
Or.. you could just do it the other way around:
http://json2csharp.com/
Throw your Json in there and it will spit out C# classes.. thereby mimicking the Json format.. not you having to manually trial-and-error it.
Upvotes: 5