-4

I have a Windows application that gets data from a webservice.

I need to use JSON to post or get data from the webservice.

What is the best way to do that? In the webservice and in the Windows application?

Please specify in details with a code sample because I am new to JSON.

4

3 回答 3

1

使用Json.NET

您可以从 NuGet 下载并安装它。

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

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

并序列化:

string json = JsonConvert.SerializeObject(new MyObject {});

有关更多示例和说明,请参阅文档

于 2013-08-29T08:48:49.413 回答
0

It is difficult to give examples with out snippets of your classes and what you are trying to achieve.

However take a look at this function you could have in your webservice

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
        }

    }
}
于 2013-08-29T09:07:43.457 回答
0

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

于 2013-08-29T08:54:13.693 回答