想要更改JSON格式(Want to change JSON Format)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
想要更改JSON格式(Want to change JSON Format)

我有这门课

public class Contact { #regionContact Info public Guid ContactID { get; set; } public string Name { get; set; } public string RegID { get; set; } public string MobileNumber { get; set; } public string Tel { get; set; } #endregion }

当我调用getAllContact()方法时,我得到了这个结果

[{"ContactID":"7abe6291-43f2-e411-b150-000c2975315f","Name":"Visitor 1","RegID":"1","MobileNumber":"1122334455","Tel":"1122334455"},{"ContactID":"f76f310f-a3f3-e411-b150-000c2975315f","Name":"Visitor 2","RegID":"2","MobileNumber":null,"Tel":null},{"ContactID":"9b3e6018-a3f3-e411-b150-000c2975315f","Name":"Visitor 3","RegID":"3","MobileNumber":null,"Tel":null}]

但我想要的是这种格式。

{"contacts":[{"ContactID":"7abe6291-43f2-e411-b150-000c2975315f","Name":"Visitor 1","RegID":"1","MobileNumber":"1122334455","Tel":"1122334455"},{"ContactID":"f76f310f-a3f3-e411-b150-000c2975315f","Name":"Visitor 2","RegID":"2","MobileNumber":null,"Tel":null},{"ContactID":"9b3e6018-a3f3-e411-b150-000c2975315f","Name":"Visitor 3","RegID":"3","MobileNumber":null,"Tel":null}]}

如何更改以获得此json格式? 请问有人帮帮我吗?

I have this class

public class Contact { #regionContact Info public Guid ContactID { get; set; } public string Name { get; set; } public string RegID { get; set; } public string MobileNumber { get; set; } public string Tel { get; set; } #endregion }

when I call getAllContact() method, I get this result

[{"ContactID":"7abe6291-43f2-e411-b150-000c2975315f","Name":"Visitor 1","RegID":"1","MobileNumber":"1122334455","Tel":"1122334455"},{"ContactID":"f76f310f-a3f3-e411-b150-000c2975315f","Name":"Visitor 2","RegID":"2","MobileNumber":null,"Tel":null},{"ContactID":"9b3e6018-a3f3-e411-b150-000c2975315f","Name":"Visitor 3","RegID":"3","MobileNumber":null,"Tel":null}]

but what I want is with this kind of format.

{"contacts":[{"ContactID":"7abe6291-43f2-e411-b150-000c2975315f","Name":"Visitor 1","RegID":"1","MobileNumber":"1122334455","Tel":"1122334455"},{"ContactID":"f76f310f-a3f3-e411-b150-000c2975315f","Name":"Visitor 2","RegID":"2","MobileNumber":null,"Tel":null},{"ContactID":"9b3e6018-a3f3-e411-b150-000c2975315f","Name":"Visitor 3","RegID":"3","MobileNumber":null,"Tel":null}]}

How can I change to get this json format? Could anybody help me please?

最满意答案

你的getAllContact()方法应该是一个带有一个参数的对象,称为联系人,类型为List

public class ContactList { public List<Contact> contacts { get; set; } }

然后序列化ContactList对象。

Your getAllContact() method should an object with one parameter in it called contacts which is of type List

public class ContactList { public List<Contact> contacts { get; set; } }

Then serialise the ContactList object.

更多推荐

本文发布于:2023-04-17 08:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/44902229074d45287116b76345ef4de4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   JSON   Format   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!