我有以下问题。我需要在开始时返回带有 2 个属性的 JSON,并且 1 属性将具有从表中读取的多个值。另一种只是返回一个代码。您能否指导我完成在一个类实例中返回这两个对象的步骤。JSON 需要看起来像这样。
和标签必须是 2 个对象GetAllCustomersResult
。FoundData
{
"GetAllCustomersResult": [
{
"City": "Kimberley",
"CompanyName": "My Company",
"CustomerID": "Mary"
},
{
"City": "London",
"CompanyName": "My Company",
"CustomerID": "Delia"
},
{
"City": "Miami",
"CompanyName": "My Company",
"CustomerID": "Haley"
}
]
}
这是我的代码。
public class Service1 : IService1
{
public wsCustomer[] GetAllCustomers()
{
NorthwindDataContext dc = new NorthwindDataContext();
List<wsCustomer> results = new List<wsCustomer>()
{
new wsCustomer { CustomerID = "Mary", CompanyName = "My Company", City = "Kimberley" },
new wsCustomer { CustomerID = "Delia", CompanyName = "My Company", City = "London" },
new wsCustomer { CustomerID = "Haley", CompanyName = "My Company", City = "Miami" }
};
return results.ToArray();
}
public string founddata()
{
string foundCust = "1";
return foundCust;
}
}
}
我的运营合同:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getAllCustomers")]
wsCustomer[] GetAllCustomers();