How to create Json object in mvc 3 controller?

  • How would you create this Json object from my ASP.Net MVC 3 Controller?
  • New to Json, in this Json object notation, can I replace sign [

    and ]

    sign {

    and }


    var data = [
            label: 'node1',
            children: [
                { label: 'child1' },
                { label: 'child2' }
            label: 'node2',
            children: [
                { label: 'child3' }



source to share

2 answers

you have a JavascriptSerializer object in .NET that can serialize most types


and no, you cannot replace [with {(obviously you can, but almost all JSON desrializers other than what you wrote yourself could not then understand the data)



You can use Newtonsoft dll to serialize and deserialize JSON object. See below for details.

To create a JSON object, you need to add the Newtonsoft.Json DLL and the following code.

 string responseRGCDTO = JsonConvert.SerializeObject(rgcDTO);




All Articles