Home > Enterprise >  JObject Not Parsing Values I need
JObject Not Parsing Values I need

Time:12-26

String Contents =
{
"links":[
{
".tag":"file",
"url":"myURL",
"id":"CCCCC",
"name":"CCCC",
"path_lower":"CCCC"
},
{"url".. and so on.
}

JObject json = JObject.Parse(contents);
Console.WriteLine(json.GetValue("links.url"));

I am trying to get all the URL values and store them into an array. The problem is that this code does not parse anyhting. The main json is Links and the rest is under it. How can I go about getting all the URL values?

CodePudding user response:

  1. Take json["links"] as JArray.
  2. Use Linq to retrieve url from the element in (1) and cast it to string.
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

JObject json = JObject.Parse(contents);
JArray array = json["links"] as JArray;

List<string> links = array.Select(x => (string)x["url"]).ToList();

Sample demo on .NET Fiddle

  • Related