using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
""name"": ""Joe Schmoe"",
""message"": ""War does not determine who is right, only who is left."",
""created_time"": ""2017-05-05T11:33:51Z"",
""message"": ""Those who live in glass houses should not throw stones."",
""created_time"": ""2017-05-06T12:05:45Z"",
FacebookFeed feed = JsonConvert.DeserializeObject<FacebookFeed>(json);
string json2 = JsonConvert.SerializeObject(feed, Formatting.Indented);
Console.WriteLine(json2);
public class JsonPathAttribute : Attribute
public JsonPathAttribute(string jsonPath)
public string JsonPath { get; set; }
public class JsonPathConverter : JsonConverter
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
var jo = JObject.Load(reader);
object targetObj = Activator.CreateInstance(objectType);
foreach (var prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite))
var att = prop.GetCustomAttributes(true).OfType<JsonPathAttribute>().FirstOrDefault();
var jsonPath = (att != null ? att.JsonPath : prop.Name);
var token = jo.SelectToken(jsonPath);
if (token != null && token.Type != JTokenType.Null)
var value = token.ToObject(prop.PropertyType, serializer);
prop.SetValue(targetObj, value, null);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public override bool CanConvert(Type objectType)
public override bool CanWrite
[JsonConverter(typeof(JsonPathConverter))]
public class FacebookFeed
Posts = new List<FacebookFeedPost>();
public string Name { get; set; }
public int Likes { get; set; }
public List<FacebookFeedPost> Posts { get; set; }
[JsonConverter(typeof(JsonPathConverter))]
public class FacebookFeedPost
public string Id { get; set; }
public string Message { get; set; }
[JsonPath("created_time")]
public DateTime Date { get; set; }
[JsonPath("comments.summary.total_count")]
public int Comments { get; set; }