Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json反序列化问题c#_C#_Json_Facebook - Fatal编程技术网

Json反序列化问题c#

Json反序列化问题c#,c#,json,facebook,C#,Json,Facebook,我想尝试反序列化以下Facebook帖子回复: "data": [ { "id": "...", "from": { "category": "Local business", "name": "...", "id": "..." }, "message": "...", "picture": "...", "likes": { "data"

我想尝试反序列化以下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; }
} 
在反序列化json时,我想将likes.data条目映射到likes列表。我如何做到这一点???

您可以使用它来反序列化json

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

您可以使用
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; }
}
[DataContract]
公共类JsonRoot
{
[DataMember(Name=“data”)]
公共列表数据{get;set;}
}
[数据合同]
公营职位
{
[数据成员(Name=“id”)]
公共字符串Id{get;set;}
[DataMember(Name=“from”)]
来自{get;set;}的公共
[DataMember(Name=“message”)]
公共字符串消息{get;set;}
[DataMember(Name=“picture”)]
公共字符串图片{get;set;}
[DataMember(Name=“likes”)]
公共数据容器{get;set;}
}
[数据合同]
公共类数据容器
{
[DataMember(Name=“data”)]
公共列表数据{get;set;}
}
[数据合同]
公共课
{
[DataMember(Name=“category”)]
公共字符串类别{get;set;}
[数据成员(Name=“Name”)]
公共字符串名称{get;set;}
[数据成员(Name=“id”)]
公共字符串Id{get;set;}
}
[数据合同]
公共类
{
[数据成员(Name=“id”)]
公共字符串Id{get;set;}
[数据成员(Name=“Name”)]
公共字符串名称{get;set;}
}

同样的基本结构也适用于Newtonsoft Json库,但您需要将属性与其对应的Newtonsoft替代项切换出去。

或者,感谢:

    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; }
    }
使用:


“嗯,

告诉我们您是如何定义
之类的
class@YuvalItzchakov请检查大小写是否需要与整个json匹配。将
JsonProperty
属性添加到所有属性中,并使用适当的小写字母。@YuvalItzchakov我不明白你想表达什么。您可以用一些例子来详细说明吗?只需使用
JsonProperty
属性装饰整个类,并传入小写名称:
[JsonProperty(id)]
[JsonProperty(name)]
    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");