次の問題があります。最初に 2 つのプロパティを含む JSON を返す必要があり、1 つのプロパティにはテーブルから読み取られる複数の値が含まれます。もう 1 つはコードを返すだけです。これら 2 つのオブジェクトを 1 つのクラス インスタンスで返す手順を教えてください。JSON は次のようにする必要があります。
GetAllCustomersResult
とFoundData
タグは 2 つのオブジェクトである必要があります。
{
"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();