You are on the right track. Here are the corrections you need to make:
- You iterate over top-level children, expecting to get data that is actually at the same level below. You need to go to the value of the
MobileSiteContents property and iterate over the children of this. - When you take
Children() JObject , use an overload that allows them to be assigned to JProperty objects; which will facilitate the extraction of the necessary data. - Get
culture from Name of JProperty element - To get
urls , enter the Value of the JProperty element and use ToObject<string[]>() to convert it to an array of strings.
Here is the corrected code:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { var jObject = JObject.Parse(json); var result = new List<MobileSiteContentsContentSectionItem>(); foreach (var item in jObject["MobileSiteContents"].Children<JProperty>()) { var culture = item.Name; string[] urls = item.Value.ToObject<string[]>(); result.Add(new MobileSiteContentsContentSectionItem { Culture = culture, Urls = urls }); } return result; }
If you need a short code, you can reduce it to "single-line":
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { return JObject.Parse(json)["MobileSiteContents"] .Children<JProperty>() .Select(prop => new MobileSiteContentsContentSectionItem { Culture = prop.Name, Urls = prop.Value.ToObject<string[]>() }) .ToList(); }
Demo:
class Program { static void Main(string[] args) { string json = @" { ""MobileSiteContents"": { ""au/en"": [ ""http://www.url1.com"", ""http://www.url2.com"", ], ""cn/zh"": [ ""http://www.url2643.com"", ] } }"; foreach (MobileSiteContentsContentSectionItem item in Parse(json)) { Console.WriteLine(item.Culture); foreach (string url in item.Urls) { Console.WriteLine(" " + url); } } } public static IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { return JObject.Parse(json)["MobileSiteContents"] .Children<JProperty>() .Select(prop => new MobileSiteContentsContentSectionItem() { Culture = prop.Name, Urls = prop.Value.ToObject<string[]>() }) .ToList(); } public class MobileSiteContentsContentSectionItem : ContentSectionItem { public string[] Urls { get; set; } } public abstract class ContentSectionItem { public string Culture { get; set; } } }
Output:
au/en http://www.url1.com http://www.url2.com cn/zh http://www.url2643.com
source share