WCF-如何在JSON中序列化和反序列化? [英] WCF - How to serialize and deserialize in JSON?

查看:127
本文介绍了WCF-如何在JSON中序列化和反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WCF中编写了很少的类(数据协定和服务协定),并且我试图在JSON中进行序列化和反序列化.如果我需要以下JSON结构,我将如何创建DataContract:

{
  "response": {
    "locations": {
      "location": [
        {
          "id": "12",
          "name": "Hello",
          "statusid": "78"
        },
        {
          "id": "5",
          "name": "Ann",
          "statusid": "8"
        }
      ]
    },
    "error": "404 error"
  }
}

上面的结构非常简单明了,在某些位置下面可能有一些上述的位置详细信息.因此,我需要获得一个数组/列表以位置"数据成员,如下所述.目前,我只有以下DataContract:

[DataContract]
    public class Response
    {
        [DataMember]
        public string locations { get; set; }

        [DataMember]
        public string error{ get; set; }
    }

请让我知道我该如何解决?

解决方案

您要查找的完整对象的结构应为:

[DataContract(Name="response")]
public class Response
{
    [DataMember(Name = "locations")]
    public IEnumerable<Location> Locations { get; set; }

    [DataMember(Name = "error")]
    public string Error { get; set; }
}

[DataContract(Name = "location")]
public class Location
{
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "statusid")]
    public string StatusId { get; set; }
}

您需要设置对象层次结构,如{}所示,以及IEnumerable/数组属性,如所需的JSON输出中的[]所示. /p>

该网站可能会令人困惑,因为没有简单的示例,但是请查阅 JSON简介以获得基本信息了解语法.我附带的另一个很好的站点,仅带有一些简单的示例,是

我在这里将它们添加到了第二个代码块中,因为这样的结构看起来不必要地复杂.但是,如果您可以更改JSON的预期输出,那么我将选择第一个块,并进行其他更改,将两个Id列设置为int类型.

创建这些类型是为了支持类似于 WCF服务以返回JSON上的设置的服务,并使用以下代码进行了测试:

string json;
using (var ms = new MemoryStream())
{
    var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(ResponseParent));
    ser.WriteObject(ms, r);
    json = System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length)); 
}

DataContractJsonSerializer

请注意,如果您可以选择设置RESTful Web服务,则可以遵循

The structure above is pretty straight forward and under locations there can be several location details as mentioned above. So i need to get an array/list to "locations" data members as mentioned below. At the moment i have the following DataContract only:

[DataContract]
    public class Response
    {
        [DataMember]
        public string locations { get; set; }

        [DataMember]
        public string error{ get; set; }
    }

Please let me know how i can resolve this?

The full objects you are looking for should be structured as:

[DataContract(Name="response")]
public class Response
{
    [DataMember(Name = "locations")]
    public IEnumerable<Location> Locations { get; set; }

    [DataMember(Name = "error")]
    public string Error { get; set; }
}

[DataContract(Name = "location")]
public class Location
{
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "statusid")]
    public string StatusId { get; set; }
}

You need to setup the object hierarchy, as indicated by the { and }, as well as the IEnumerable / array properties, as indicated by the [ and ] from your desired JSON output.

The site can be confusing, as there are no simple examples, but please review Introducing JSON for a basic understanding of the syntax. Another good site I came along, with just some simple examples, was JSON and XML Serialization in ASP.NET Web API.

Thanks to some guidance by vittore, I noticed that to build the exact match to your JSON output, you will need objects like:

[DataContract]
public class ResponseParent
{
    [DataMember(Name = "response")]
    public Response ResponseInstance { get; set; }
}

[DataContract]
public class Response
{
    [DataMember(Name = "locations")]
    public LocationCollectionIntermediate Locations { get; set; }

    [DataMember(Name = "error")]
    public string Error { get; set; }
}

[DataContract]
public class LocationCollectionIntermediate
{
    [DataMember(Name = "location")]
    public IEnumerable<Location> Locations { get; set; }
}

[DataContract]
public class Location
{
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "statusid")]
    public string StatusId { get; set; }
}

I've added these into a second code block here, because such a structure seems needlessly complicated. However, if you are in a position where you can change the expected output of your JSON, I'd go with the first block, with the additional change of making the two Id columns into int types.

These types were created to support a service similar to as setup at WCF Service to return JSON, and tested using the following code:

string json;
using (var ms = new MemoryStream())
{
    var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(ResponseParent));
    ser.WriteObject(ms, r);
    json = System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length)); 
}

Details on DataContractJsonSerializer

Note also that if you have the option to setup a RESTful web service, then you can follow the guidelines of How to create a JSON WCF RESTful Service in 60 seconds.

这篇关于WCF-如何在JSON中序列化和反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆