我可以让 JavaScriptSerializer 分层序列化 LINQ 结果吗?
我正在这样做:
var data = from a in attributes
from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
.DefaultIfEmpty(new DocClassAttributeFieldItem())
select new
{
Id = a.Id,
LabelText = a.LabelText,
Items = i
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);
结果是这样的:
[{
"Id": 1,
"LabelText": "Unit On-Line Status:",
"Items": {
"Id": 1,
"DocClassAttributeFieldId": 1,
"LabelText": "Online",
"ValueText": "Online",
"Ordering": 1
}
},
{
"Id": 1,
"LabelText": "Unit On-Line Status:",
"Items": {
"Id": 2,
"DocClassAttributeFieldId": 1,
"LabelText": "Offline",
"ValueText": "Offline",
"Ordering": 2
},
}]
我希望得到这样的结果:
[{
"Id": 1,
"LabelText": "Unit On-Line Status:",
"Items": [{
"Id": 1,
"DocClassAttributeFieldId": 1,
"LabelText": "Online",
"ValueText": "Online",
"Ordering": 1
},{
"Id": 2,
"DocClassAttributeFieldId": 1,
"LabelText": "Offline",
"ValueText": "Offline",
"Ordering": 2
}]
}]
使用 JavaScriptSerializer 可以轻松完成此操作吗?或者可以重新设计 LINQ 语句来生成此结果吗?
更新:感谢一些类似的帖子...
http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
http://encosia.com/using-complex-types- to-make-calling-services-less-complex/
我不会使用 JavaScriptSerializer,ASP.Net 会为我完成这一切:
[WebMethod]
public static object GetDocClass(int docClassId)
{
var data = from a in attributes
select new
{
Id = a.Id,
LabelText = a.LabelText,
Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)
};
return data;
}
I'm doing this:
var data = from a in attributes
from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
.DefaultIfEmpty(new DocClassAttributeFieldItem())
select new
{
Id = a.Id,
LabelText = a.LabelText,
Items = i
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);
The result is this:
[{
"Id": 1,
"LabelText": "Unit On-Line Status:",
"Items": {
"Id": 1,
"DocClassAttributeFieldId": 1,
"LabelText": "Online",
"ValueText": "Online",
"Ordering": 1
}
},
{
"Id": 1,
"LabelText": "Unit On-Line Status:",
"Items": {
"Id": 2,
"DocClassAttributeFieldId": 1,
"LabelText": "Offline",
"ValueText": "Offline",
"Ordering": 2
},
}]
I'd like to have a result like this:
[{
"Id": 1,
"LabelText": "Unit On-Line Status:",
"Items": [{
"Id": 1,
"DocClassAttributeFieldId": 1,
"LabelText": "Online",
"ValueText": "Online",
"Ordering": 1
},{
"Id": 2,
"DocClassAttributeFieldId": 1,
"LabelText": "Offline",
"ValueText": "Offline",
"Ordering": 2
}]
}]
Can this be done easily with JavaScriptSerializer or can the LINQ statement be reworked to produce this?
Update: Thanks to some posts like these...
http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
http://encosia.com/using-complex-types-to-make-calling-services-less-complex/
I'm not going to use the JavaScriptSerializer, ASP.Net does it all for me:
[WebMethod]
public static object GetDocClass(int docClassId)
{
var data = from a in attributes
select new
{
Id = a.Id,
LabelText = a.LabelText,
Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)
};
return data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做,但需要使用组来填充项目。
这是一个突然的想法,所以如果它不适合你,请告诉我。
You can do it but you need to use a group into to populate items.
That was a shot from the hip, so let me know if it doesn't work for you.
可能有更好的方法,但尼克斯的回答帮助我想出了这个:
There is probably a better way, but Nix's answer helped me come up with this: