想要更改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.
更多推荐
发布评论