【问题标题】:WCF REST Return JSON with 2 properties [closed]WCF REST返回具有2个属性的JSON [关闭]
【发布时间】:2013-01-28 12:24:28
【问题描述】:

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

GetAllCustomersResultFoundData 标签必须是这两个对象。

{
"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();

【问题讨论】:

  • 非常不清楚。你有什么问题?

标签: c# .net json wcf


【解决方案1】:

只需将结果包装在另一个类中并返回那个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多