2014-10-17 20 views
1

我想嘗試反序列化下面的Facebook發佈響應反序列化的問題:雖然反序列化的Json C#

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; } 
} 

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

Post模型類json我想將likes.data條目映射到喜歡列表。我怎樣才能做到這一點??

+0

向我們展示你如何定義'Like'類 – 2014-10-17 13:57:04

+0

@YuvalItzchakov請更新問題 – user1740381 2014-10-17 14:00:25

+0

套管需要匹配您的整個JSON。將JsonProperty屬性添加到所有屬性中,並帶有適當的下方框。 – 2014-10-17 14:05:38

回答

0

你可以使用Newtonsoft.Json來反序列化json。具有以下

反序列化:

Newtonsoft.Json.JsonConvert.DeserializeObject("{\"data\": [...]") 
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選項來切換屬性。

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,