【问题标题】:How to translate PHP JSON web service to C#?如何将 PHP JSON Web 服务转换为 C#?
【发布时间】:2011-06-01 11:22:37
【问题描述】:

我一直在编写一个可以将 JSON 发送到 PHP 文件的 iPhone 应用程序,并且在这方面相当成功。但是,现在我想继续使用 ASP C# 并创建一个等效的 .NET Web 服务,类似于我之前编写的用于解码和编码 JSON 的 PHP,我完全一无所知。

在 PHP 中我这样做:

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);

接收 JSON POST 数据并对其进行解码。然后使用这个:

$data = $decoded;
header('Content-Type: application/json');
echo json_encode($data);

如何编写等效的 C# Web 服务?谢谢。

【问题讨论】:

    标签: c# php web-services json


    【解决方案1】:

    使用 WCF REST 非常简单。按照以下链接的步骤操作:

    http://blogs.msdn.com/b/kaevans/archive/2008/04/03/creating-restful-services-using-wcf.aspx

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebGet(UriTemplate="customers/{id}", ResponseFormat=WebMessageFormat.Json)]
        Customer GetCustomer(string id);
    
        [OperationContract]
        [WebInvoke(UriTemplate="customers", ResponseFormat=WebMessageFormat.Json)]
        Customer PostCustomer(Customer c);
    }
    

    因此结果将通过 WCF 以 Json 格式编码。

    【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 2013-05-30
    • 2012-02-08
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 2016-07-01
    相关资源
    最近更新 更多