using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
public static void Main()
""LinkType1"": ""google"",
""LinkUrl1"": ""https://plus.google.com/test"",
""LinkType2"": ""facebook"",
""LinkUrl2"": ""https://www.facebook.com/test"",
""LinkType3"": ""linkedin"",
""LinkUrl3"": ""http://www.linkedin.com/test"",
""errorMessage"": ""Success""
LinksResult result = JsonConvert.DeserializeObject<LinksResult>(json);
foreach (LinkData link in result.PageItems)
Console.WriteLine(string.Format("{0}: {1} ({2})", link.LinkType, link.LinkUrl, (link.LinkShow ? "show" : "hide")));
Console.WriteLine("Error: " + ex.ToString());
public interface IPage<TItem>
List<TItem> PageItems { get; set; }
public class PageConverter<TPage, TItem> : JsonConverter
where TPage : IPage<TItem>, new()
private readonly Regex _numberPostfixRegex = new Regex(@"\d+$");
public override bool CanWrite
public override bool CanConvert(Type objectType)
return (objectType == typeof(TPage));
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var obj = serializer.Deserialize<JObject>(reader);
serializer.Populate(obj.CreateReader(), page);
page.PageItems = new List<TItem>();
for (int i = 1; i <= page.Count; i++)
string index = i.ToString();
var jsonItem = new JObject();
foreach (var prop in obj.Properties().Where(p => _numberPostfixRegex.Match(p.Name).Value == index))
jsonItem[_numberPostfixRegex.Replace(prop.Name, "")] = prop.Value;
TItem item = jsonItem.ToObject<TItem>(serializer);
page.PageItems.Add(item);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
[JsonConverter(typeof(PageConverter<LinksResult, LinkData>))]
public class LinksResult : IPage<LinkData>
public int Count { get; set; }
public List<LinkData> PageItems { get; set; }
public string LinkType { get; set; }
public string LinkUrl { get; set; }
public bool LinkShow { get; set; }