WCF REST返回具有2个属性的JSON

时间:2013-01-28 12:24:28

标签: c# .net json wcf

我有以下问题。我需要在start时返回带有2个属性的JSON,并且1属性将具有从表中读取的多个值。另一种只是返回一个代码。您能否指导我完成在一个类实例中返回这两个对象的步骤。 JSON需要看起来像这样。

GetAllCustomersResultFoundData代码必须是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();

1 个答案:

答案 0 :(得分:1)

将结果包装在另一个类中并返回该结果:

public class Results
{
    public wsCustomer[] Customers {get;set;}
    public int Result {get;set;}
}