Create nested json with c#

旧城冷巷雨未停 提交于 2019-12-04 10:05:30

Are you using MVC 3? - Do something like:

return Json(myObectWithListProperties, JsonRequestBehavior.AllowGet);

I use this to return complex C# objects that match the structure of the JavaScript objects I want.

e.g.:

var bob = new {
    name = "test",
    orders = new [] {
        new  { itemNo = 1, description = "desc" },
        new  { itemNo = 2, description = "desc2" }
    }
};

return Json(bob, JsonRequestBehavior.AllowGet);

gives:

{
    "name": "test",
    "orders": [
        {
            "itemNo": 1,
            "description": "desc"
        },
        {
            "itemNo": 2,
            "description": "desc2"
        }
    ]
}

EDIT: A bit more nesting for fun:

var bob = new {
    name = "test",
    orders = new [] {
        new  { itemNo = 1, description = "desc" },
        new  { itemNo = 2, description = "desc2" }                  
    },
    test = new {
        a = new {
            b = new {
                something = "testing",
                someOtherThing = new {
                    aProperty = "1",
                    another = "2",
                    theThird = new {
                        bob = "quiteDeepNesting"
                    }
                }
            }
        }
    }
};

return Json(bob, JsonRequestBehavior.AllowGet);

gives:

{
    "name": "test",
    "orders": [
        {
            "itemNo": 1,
            "description": "desc"
        },
        {
            "itemNo": 2,
            "description": "desc2"
        }
    ],
    "test": {
        "a": {
            "b": {
                "something": "testing",
                "someOtherThing": {
                    "aProperty": "1",
                    "another": "2",
                    "theThird": {
                        "bob": "quiteDeepNesting"
                    }
                }
            }
        }
    }
}

Try using

using System.Web.Script.Serialization;

//Assumed code to connect to a DB and get data out using a Reader goes here

Object data = new {
    a = reader.GetString(field1),
    b = reader.GetString(field2),
    c = reader.GetString(field3)
};
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string json = javaScriptSerializer.Serialize(data);

This is built-in and saves you the work of serializing to JSON yourself!

This example assumes you are getting data from a database using some sort of reader, and it then constructs the object you want to serialize using an anonymous class. Your anonymous class can be as simple or complex as you need it to be and the JavaScriptSerializer will handle transforming it to JSON. This approach is also useful because you can easily control the JSON property names it will create in the JSON.

Ozesh

You can make use of the ExpandoObject under the System.Dynamic namespace.

Here is a small snippet for achieving your solution:

dynamic parameters = new dynamic[2];

parameters[0] = new ExpandoObject();
parameters[0].title = "Yes";
parameters[0].id = "1";

parameters[0].menu = new dynamic[1];
parameters[0].menu[0] = new ExpandoObject();

parameters[0].menu[0].title = "Maybe";
parameters[0].menu[0].id = "3";
parameters[0].menu[0].alert = "No";
parameters[0].menu[0].menu = new dynamic[1];
parameters[0].menu[0].menu[0] = new ExpandoObject();
parameters[0].menu[0].menu[0].title = "Maybe Not";
parameters[0].menu[0].menu[0].id = "8";
parameters[0].menu[0].menu[0].alert = "No";
parameters[0].menu[0].menu[0].menu = new dynamic[0];

parameters[1] = new ExpandoObject();
parameters[1].title = "No";
parameters[1].id = "2";
parameters[1].menu = new dynamic[0];


string json = JsonConvert.SerializeObject(parameters, Formatting.Indented);
Console.WriteLine(json);

Here is the work in fiddle

Note: There are other ways to achieve this, but I have been using this approach.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!