您使用MVC 3? - 做这样的事情:
return Json(myObectWithListProperties, JsonRequestBehavior.AllowGet);
我用它来返回复杂的c#对象,匹配我想要的javascript对象的结构。
例如为:
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"
}
}
}
}
}
}
您是否使用JSON.Net库或自定义实现?检查图书馆是否可以提供帮助 - 将为您节省大量时间... – Sunny 2012-04-27 16:11:15
您将什么定义为*嵌套字符串*? – 2012-04-27 16:13:02
@cWilk,你应该张贴您的代码(预计1条或2号线),显示创建JSON你选择什么特别的“非常容易”的方式。从SP – 2012-04-27 16:16:17