用c#创建嵌套的json

时间:2012-04-27 16:08:51

标签: c# .net json nested

我可以使用c#

轻松创建平面序列化JSON字符串

我的问题是我想创建一个如下所示的嵌套字符串

[ { 
    title: "Yes",
    id : "1",
    menu: [ { 
        title: "Maybe",
        id : "3",
        alert : "No",
        menu: [ {
            title: "Maybe Not",
            id : "8",
            alert : "No",
            menu: []
        } ]
    } ]
},
{
    title: "No",
    id : "2",
    menu: []
}]

任何帮助都会很棒

4 个答案:

答案 0 :(得分:13)

您使用的是MVC 3吗? - 做类似的事情:

return Json(myObectWithListProperties, JsonRequestBehavior.AllowGet);

我用它来返回符合我想要的JavaScript对象结构的复杂C#对象。

e.g:

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

return Json(bob, JsonRequestBehavior.AllowGet);

给出:

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

编辑:为了好玩而筑巢一点:

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);

给出:

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

答案 1 :(得分:4)

尝试使用

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);

这是内置的,可以为您节省序列化到JSON的工作!

此示例假定您使用某种类型的读取器从数据库获取数据,然后使用匿名类构造要序列化的对象。您的匿名类可以像您需要的那样简单或复杂,JavaScriptSerializer将处理它转换为JSON。这种方法也很有用,因为您可以轻松地控制它将在JSON中创建的JSON属性名称。

答案 2 :(得分:1)

您可以使用ExpandoObject命名空间下的System.Dynamic

以下是实现解决方案的小片段:

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

注意:还有其他方法可以实现这一目标,但我一直在使用这种方法。

答案 3 :(得分:0)

using System.Web.Script.Serialization;
 


var strNJson = new
            {
                to = "hello",
                notification = new
                {
                    title = "textTitle",
                    body = "bodyText"
                }
            };
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            string json = javaScriptSerializer.Serialize(strNJson);
{   "to":"hello",
       "notification": {
                        "title":"titleText",
                         "body":"bodyText"
                     } 
}