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:
- Take
json["links"]
asJArray
. - Use Linq to retrieve
url
from the element in (1) and cast it tostring
.
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();