Json字符串格式问题 [英] Json string format question
问题描述
嗨
这是我的json字符串
this is my json string
[{"Nid":1,"Name":"user1","Info":{"Age":10,"Address":"xxxxxxx","Note":"rrrrrr"}},{"Nid":2,"Name":"user2","Info":{"Age":20,"Address":"sssssss","Note":"ccccccc"}},{"Nid":3,"Name":"user3","Info":{"Age":30,"Address":"rrrrrrr","Note":"vvvvvvv"}},{"Nid":4,"Name":"user4","Info":{"Age":40,"Address":"aaaaa","Note":"bbbbb"}}]
这是我的代码,使用EntityFrameworkCore
and this is my code, use EntityFrameworkCore
return Json(await _context.UserInfo.Select(u => new { Nid = u.Nid, Name = u.Name,Info=new { Age=u.Age,Address=u.Address,Note=u.Note } }).ToListAsync());
现在我想生成像这样的json字符串
now I want generate json string like this
< span style ="font-family:monospace;字体大小:11.9925px; white-space:pre-wrap"> [{" Nid":1,"Name":"user1","Info": [ {" Age":10," ;地址":" xxxxxxx"," Note":" rrrrrr"} ] },{" Nid":2," Name":" user2"," Info" ;: [ {" Age":20," Address":" sssssss"," Note":" ccccccc"} ] },{" ; Nid的":3,"姓名":"用户3","详情":的 [强> {"年龄":30,"地址]按钮:" RRRRRRR","注意" ;:" vvvvvvv"} ] },{" Nid":4," Name":" user4"," Info": [ {" ;年龄":40,"地址":"aaaaa","注意":"bbbbb"} ] }]
[{"Nid":1,"Name":"user1","Info": [ {"Age":10,"Address":"xxxxxxx","Note":"rrrrrr"} ] },{"Nid":2,"Name":"user2","Info": [ {"Age":20,"Address":"sssssss","Note":"ccccccc"} ] },{"Nid":3,"Name":"user3","Info":[{"Age":30,"Address":"rrrrrrr","Note":"vvvvvvv"} ] },{"Nid":4,"Name":"user4","Info":[{"Age":40,"Address":"aaaaa","Note":"bbbbb"} ] }]
我如何更改我的代码?(在"信息"栏添加[]谢谢!!
How do i do change my code ? (add [] at "info" column) thanks !!
推荐答案
是的,info需要是一个数组,所以请将数据包装起来。
Yes, info needs to be an array so wrap the data as such.
Info = new [] {
new { Age = u.Age, Address = u.Address, ... }
}
这篇关于Json字符串格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!