1

I want trying to deserialize the following Facebook post response:

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

Post model class is:

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

Like model class

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

While deserializing the json i want to map likes.data entries to Likes list. How can I do this??

user1740381
  • 2,121
  • 8
  • 37
  • 61

3 Answers3

0

you can use Newtonsoft.Json for deserializing json.

Newtonsoft.Json.JsonConvert.DeserializeObject("{\"data\": [...]")
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
0

You can use the DataContractJsonSerializer (System.Runtime.Serialization) to deserialize this:

Deserialize with the following:

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

Use types like this:

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

This same basic structure should work for the Newtonsoft Json library too, but you'll need to switch out the attributes with their corresponding Newtonsoft alternatives.

Nick Gotch
  • 9,167
  • 14
  • 70
  • 97
0

Alternatively, thanks to:

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

you could also write:

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

and:

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

to test/verify:

        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");

using:

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

'HTH,

YSharp
  • 1,066
  • 9
  • 8