You must Sign In to post a response.
  • Category: ASP.NET

    Construct JSON string in c#

    Hi Team



    I have the dataset data but i need to format json like below code using c#

    {
    "name": "TOPICS",
    "children": [
    {
    "name": "Topic A",
    "children": [
    {
    "name": "Sub A1",
    "size": 5,
    "text": "A story",
    "sentiment": 0.8,
    "source": "dictionary"
    },
    {
    "name": "Sub A2",
    "size": 5,
    "text": "A note",
    "sentiment": 0.3,
    "source": "dictionary"
    }
    ]
    },
    {
    "name": "Topic B",
    "children": [
    {
    "name": "Sub B1",
    "size": 5,
    "text": "A vignette",
    "sentiment": 0.5,
    "source": "newspaper"
    },
    {
    "name": "Sub B2",
    "size": 3,
    "text": "A tall-tale",
    "sentiment": 0.2,
    "source": "friend"
    },
    {
    "name": "Sub B3",
    "size": 4,
    "text": "A joke",
    "sentiment": 0.8,
    "source": "email"
    }
    ]
    },
    {
    "name": "Topic C",
    "children": [
    {
    "name": "Sub A1",
    "size": 4,
    "text": "A narrative",
    "sentiment": 0.2,
    "source": "newspaper"
    },
    {
    "name": "Sub A2",
    "size": 4,
    "text": "A chronology",
    "sentiment": 0.3,
    "source": "email"
    }
    ]
    }
    ]
    }
    Can anyone help me?

    Regards,
    Saravana
  • #769822
    I will suggest you to make a data contract as per the JSON structure you need. Once your data contract is ready, you can just add the values that you want to punch and then use the NewtonSoft JSON.Net library to serialized the object. There is very good example provided on below link

    https://stackoverflow.com/questions/11345382/convert-object-to-json-string-in-c-sharp

    JsonConvert.SerializeObject(myObject);


  • Sign In to post your comments