Json反序列化问题c#

时间:2014-10-17 13:52:30

标签: c# json facebook

我想尝试反序列化以下Facebook帖子回复:

"data": [
    {
      "id": "...", 
      "from": {
        "category": "Local business", 
        "name": "...", 
        "id": "..."
      }, 
      "message": "...", 
      "picture": "...", 
      "likes": {
        "data": [
          {
            "id": "...", 
            "name": "..."
          }, 
          {
            "id": "...", 
            "name": "..."
        ]
      }
    }
]

发布模型类是:

public class Post
{
  public string Id { get; set; }
  public From From { get; set; }
  public string Message { get; set; }
  public string Picture { get; set; }

  [JsonProperty("likes.data")]      <===== why this is not working??
  public List<Like> Likes { get; set; }
} 

与模型类一样

public class Like
{
    public string Id { get; set; }
    public string Name { get; set; }
}

在反序列化json的同时,我想将likes.data条目映射到Likes列表。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

您可以使用Newtonsoft.Json反序列化json。

Newtonsoft.Json.JsonConvert.DeserializeObject("{\"data\": [...]")

答案 1 :(得分:0)

您可以使用DataContractJsonSerializer(System.Runtime.Serialization)对此进行反序列化:

使用以下内容反序列化:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

var jsonString = "{\"data\":[{\"id\":\"...\",\"from\":{\"category\":\"Local business\",\"name\":\"...\",\"id\":\"...\"},\"message\":\"...\",\"picture\":\"...\",\"likes\":{\"data\":[{\"id\":\"...\",\"name\":\"...\"},{\"id\":\"...\",\"name\":\"...\"}]}}]}";
var jsonSerializer = new DataContractJsonSerializer(typeof(JsonRoot));
JsonRoot json = null;
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
    stream.Position = 0;
    json = (JsonRoot)jsonSerializer.ReadObject(stream);
}

使用以下类型:

[DataContract]
public class JsonRoot
{
    [DataMember(Name="data")]
    public List<Post> Data { get; set; }
}

[DataContract]
public class Post
{
    [DataMember(Name="id")]
    public string Id { get; set; }
    [DataMember(Name="from")]
    public From From { get; set; }
    [DataMember(Name="message")]
    public string Message { get; set; }
    [DataMember(Name="picture")]
    public string Picture { get; set; }
    [DataMember(Name="likes")]
    public DataContainer Likes { get; set; }
}

[DataContract]
public class DataContainer
{
    [DataMember(Name="data")]
    public List<Like> Data { get; set; }
}

[DataContract]
public class From
{
    [DataMember(Name="category")]
    public string Category { get; set; }
    [DataMember(Name="name")]
    public string Name { get; set; }
    [DataMember(Name="id")]
    public string Id { get; set; }
}

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

同样的基本结构也适用于Newtonsoft Json库,但是您需要使用相应的Newtonsoft替代品来切换属性。

答案 2 :(得分:0)

或者,感谢:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.Json; // Repo: https://github.com/ysharplanguage/FastJsonParser

你也可以写:

    public class From
    {
        public string id { get; set; }
        public string name { get; set; }
        public string category { get; set; }
    }

    public class Post
    {
      public string id { get; set; }
      public From from { get; set; }
      public string message { get; set; }
      public string picture { get; set; }
      public Dictionary<string, Like[]> likes { get; set; }
    } 

    public class Like
    {
        public string id { get; set; }
        public string name { get; set; }
    }

        var SO_26426594_input = @"{ ""data"": [
{
  ""id"": ""post 1"", 
  ""from"": {
    ""category"": ""Local business"", 
    ""name"": ""..."", 
    ""id"": ""...""
  }, 
  ""message"": ""..."", 
  ""picture"": ""..."", 
  ""likes"": {
    ""data"": [
      {
        ""id"": ""like 1"", 
        ""name"": ""text 1...""
      }, 
      {
        ""id"": ""like 2"", 
        ""name"": ""text 2...""
      }
    ]
  }
} ] }";

        var posts = new JsonParser().Parse<Dictionary<string, Post[]>>(SO_26426594_input);

测试/验证:

        System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].id == "post 1");
        System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].from.category == "Local business");
        System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].likes["data"][0].id == "like 1");
        System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].likes["data"][1].id == "like 2");

使用:

System.Text.Json (https://www.nuget.org/packages/System.Text.Json)

“HTH,