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"));
我正在嘗試獲取所有 URL 值并將它們存盤到一個陣列中。問題是這段代碼沒有決議任何內容。主要的 json 是 Links,其余的在它下面。如何獲取所有 URL 值?
uj5u.com熱心網友回復:
- 以
json["links"]作為JArray。 - 使用 Linq
url從 (1) 中的元素中檢索并將其轉換為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();
.NET Fiddle 上的示例演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394023.html
