2

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"
     }, 
.....
1
  • json.net is pretty awesome Commented Jul 25, 2013 at 2:59

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This saved me a lot of time. Thanks.
0

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.

1 Comment

Codeplex link is sadly dead. This seems like the new link: github.com/JsonCSharpClassGenerator/JsonCSharpClassGenerator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.