C# 在Webservice上使用JSON

C# 在Webservice上使用JSON,c#,.net,json,web-services,C#,.net,Json,Web Services,我有一个从Web服务获取数据的Windows应用程序 我需要使用JSON从Web服务发布或获取数据 最好的方法是什么?在Web服务和Windows应用程序中 请使用代码示例详细说明,因为我是JSON新手。使用 您可以从NuGet下载并安装它 要使用它,您需要创建一个与Json匹配的C#模型,然后调用: string json = ""; MyObject obj = JsonConvert.DeserializeObject<MyObject>(json); 有关更多示例和说明,请

我有一个从Web服务获取数据的Windows应用程序

我需要使用JSON从Web服务发布或获取数据

最好的方法是什么?在Web服务和Windows应用程序中

请使用代码示例详细说明,因为我是JSON新手。

使用

您可以从NuGet下载并安装它

要使用它,您需要创建一个与Json匹配的C#模型,然后调用:

string json = "";
MyObject obj = JsonConvert.DeserializeObject<MyObject>(json);

有关更多示例和说明,请参见。

作为Json.Net的替代方案,您可以使用本文中描述的WCF。WCF是Microsoft作为.Net的一部分提供的一个服务框架。

如果没有您的类片段和您试图实现的目标,很难给出示例

不过,看看您的Web服务中可能具有的这个功能

using Newsoft.Json;

public JsonResult FunctionName(string JsonString)
{
    if (JsonString!= null)
    {
        YourObject YourObjectInstance = new YourObject ();

        try
        {
            YourObjectInstance = JsonConvert.DeserializeObject<YourObject >(JsonString);
           //do something with the data                

           // return a Json response of either your object or another object type
           return Json(YourObjectInstance, JsonRequestBehavior.AllowGet);
        }
        catch
        {
            return new JsonResult();  //return empty JsonResult
        }
        }
        else
        {
            return new JsonResult();  //return empty JsonResult
        }

    }
}
使用Newsoft.Json;
公共JsonResult函数名(字符串JsonString)
{
if(JsonString!=null)
{
YourObject YourObjectInstance=newyourObject();
尝试
{
YourObjectInstance=JsonConvert.DeserializeObject(JsonString);
//对数据做点什么
//返回对象或其他对象类型的Json响应
返回Json(YourObjectInstance,JsonRequestBehavior.AllowGet);
}
抓住
{
返回新的JsonResult();//返回空的JsonResult
}
}
其他的
{
返回新的JsonResult();//返回空的JsonResult
}
}
}

安装后,我如何使用它,请提供更多详细信息?网上有大量示例。如果您能提供更多详细信息、类或代码示例,我相信我们可以提供更详细的答案。我只创建了一个Web服务,直到现在,我不知道如何使用json从windows应用程序发送请求,如何在webservice中接收请求,以及如何从webservice获得对windows应用程序的响应
using Newsoft.Json;

public JsonResult FunctionName(string JsonString)
{
    if (JsonString!= null)
    {
        YourObject YourObjectInstance = new YourObject ();

        try
        {
            YourObjectInstance = JsonConvert.DeserializeObject<YourObject >(JsonString);
           //do something with the data                

           // return a Json response of either your object or another object type
           return Json(YourObjectInstance, JsonRequestBehavior.AllowGet);
        }
        catch
        {
            return new JsonResult();  //return empty JsonResult
        }
        }
        else
        {
            return new JsonResult();  //return empty JsonResult
        }

    }
}