简体   繁体   English

WCF Web服务返回特殊的JSON响应

[英]WCF web service return special JSON response

I wrote an iOS app that uses a Post HTTP method via a URL and two parameters that checks the validity of the two parameters against records in a table in a mySQL database and returns a JSON response. 我编写了一个iOS应用,该应用通过URL和两个参数使用Post HTTP方法,该参数针对mySQL数据库中表中的记录检查两个参数的有效性,并返回JSON响应。

If the parameters are valid the response is {"success":1} , if not the response is {"success":0,"error_message":"The parameters are invalid."} . 如果参数有效,则响应为{"success":1} ,否则,响应为{"success":0,"error_message":"The parameters are invalid."} The web service that does this was done in PHP. 执行此操作的Web服务是用PHP完成的。 All works fine. 一切正常。

However, now I want to do the same thing against an MSSQL database using a WCF web service. 但是,现在我想对使用WCF Web服务的MSSQL数据库执行相同的操作。 I did some research and I actually have it working whereby I am using a stored procedure in the web service and the response is returned in JSON format. 我进行了一些研究,实际上可以正常工作,从而可以在Web服务中使用存储过程,并以JSON格式返回响应。

I am new to WCF web services and C#.NET and I am trying to figure out how to have the response not return the results from the stored procedure but just a JSON response indicating if there are results and another response if there are no results. 我是WCF Web服务和C#.NET的新手,我试图弄清楚如何使响应不返回存储过程的结果,而只是返回一个JSON响应(指示是否有结果)和另一个响应(如果没有结果)。 The code for my test WCF web service (Service1.svc.cs) is below and this brings back all the data in JSON, but all I want is if there is data in the result then respond with {"success":1} , and if no data in the result, then respond with {"success":0,"error_message":"The parameters are invalid."} . 下面是我的测试WCF Web服务(Service1.svc.cs)的代码,它带回了JSON中的所有数据,但是我想要的只是如果结果中有数据,则用{"success":1}响应,如果结果中没有数据,则返回{"success":0,"error_message":"The parameters are invalid."} I am not including the IService1.cs , the web.config or the CustomerOrdersOrders.cs code since I'm not sure they are required to solve my problem. 我不包括IService1.csweb.configCustomerOrdersOrders.cs代码,因为我不确定解决它们是否需要它们。 If they are, let me know and I will post them. 如果是,请告诉我,我会发布它们。

Any help is appreciated. 任何帮助表示赞赏。

public List<CustomerOrdersOrders> GetCustomerOrdersOrders(string customerID)
        {
            NorthwindDataContext dc = new NorthwindDataContext();
            List<CustomerOrdersOrders> results = new List<CustomerOrdersOrders>();
            System.Globalization.CultureInfo ci =     System.Globalization.CultureInfo.GetCultureInfo("en-US");

            foreach (CustOrdersOrdersResult oneOrder in dc.CustOrdersOrders(customerID))
            {
                results.Add(new CustomerOrdersOrders()
                {
                    OrderID = oneOrder.OrderID,
                    OrderDate = (oneOrder.OrderDate == null) ? "" :   oneOrder.OrderDate.Value.ToString("d", ci),
                    RequiredDate = (oneOrder.RequiredDate == null) ? "" :   oneOrder.RequiredDate.Value.ToString("d", ci),
                    ShippedDate = (oneOrder.ShippedDate == null) ? "" :   oneOrder.ShippedDate.Value.ToString("d", ci)

                });
            }

              return results;

        }

If you just want to test the presence of data, you have 2 options: either create a stored procedure which takes a customerid and returns a boolean, if data exists. 如果只想测试数据的存在,则有2个选项:创建一个存储过程,该过程使用一个customerid,如果数据存在,则返回一个布尔值。 OR, use a non-stored proc approach like this: 或者,使用这样的非存储proc方法:

public bool DataExists(string customerId)
{
 using (var dc = new NorthwindDataContext())
 {
  return dc.CustomerOrders.Any(co => co.CustomerID == customerId);
  // or use the boolean stored procedure
 }
}

once you have the above boolean method, you can then have your web method (operation contract in WCF) return the right JSON. 一旦有了上述的boolean方法,就可以让您的Web方法(WCF中的操作合同)返回正确的JSON。

You can lookup WCF with JSON response as to how to this: 您可以使用JSON响应查找WCF,以了解如何执行此操作:

eg 例如

http://www.wcftutorial.net/How_To_JSON_Using_WCF.aspx How do I return clean JSON from a WCF Service? http://www.wcftutorial.net/How_To_JSON_Using_WCF.aspx 如何从WCF服务返回干净的JSON?

most of the times, you need to attribute your operation contract with: 在大多数情况下,您需要将运营合同归因于:

[WebGet(ResponseFormat = WebMessageFormat.Json)]

and have a Data contract returned which maps to the JSON response you need. 并返回了数据合约,该合约映射到您所需的JSON响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM