7

我可以很容易地用 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

4 回答 4

16

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"
                    }
                }
            }
        }
    }
}
于 2012-04-27T16:13:48.293 回答
5

尝试使用

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 属性名称。

于 2012-04-27T16:15:41.340 回答
2
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"
                     } 
}
于 2020-07-17T13:07:46.093 回答
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);

这是小提琴中的工作

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

于 2018-01-23T05:59:07.157 回答