[英]C# Extract JSON from nested array
我正在嘗試使用C#和JSON.NET迭代嵌套的JSON數組。 JSON代表在線網上商店的類別 - 下面是一個例子。 我的目標是創建所有類別名稱的列表。
{
"id": 2,
"parent_id": 1,
"name": "Main Category List",
"is_active": true,
"position": 1,
"level": 1,
"product_count": 0,
"children_data": [
{
"id": 9,
"parent_id": 2,
"name": "Mens Clothing",
"is_active": true,
"position": 6,
"level": 2,
"product_count": 0,
"children_data": []
},
{
"id": 8,
"parent_id": 2,
"name": "Womens Clothing",
"is_active": true,
"position": 7,
"level": 2,
"product_count": 0,
"children_data": [
{
"id": 223,
"parent_id": 8,
"name": "Outdoor Clothing",
"is_active": true,
"position": 1,
"level": 3,
"product_count": 0,
"children_data": []
},
{
"id": 224,
"parent_id": 8,
"name": "Hiking Clothing",
"is_active": true,
"position": 2,
"level": 3,
"product_count": 0,
"children_data": []
},
{
"id": 596,
"parent_id": 8,
"name": "Dresses",
"is_active": true,
"position": 3,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 694,
"parent_id": 596,
"name": "Summer Dresses",
"is_active": true,
"position": 13,
"level": 4,
"product_count": 0,
"children_data": [
{
"id": 720,
"parent_id": 694,
"name": "Accessories",
"is_active": true,
"position": 1,
"level": 5,
"product_count": 0,
"children_data": [ ]
}
]
}
]
}
]
},
{
"id": 10,
"parent_id": 2,
"name": "Sale & Clearance",
"is_active": true,
"position": 8,
"level": 2,
"product_count": 0,
"children_data": []
}
]
}
可能存在不同級別的類別,我需要解析每個類別。 我想獲得每個類別並創建一個地圖。 例如(主要類別列表 - >女裝 - >戶外服裝)。 我想我可以檢查子數據的深度,但我不知道如何更深入地檢查下一個Json對象。
JObject responseObject = JObject.Parse(response.Content);
foreach (JObject category in getCatResponseObj.SelectToken("children_data"))
{
while loop checking depth of children_data
}
這似乎是遞歸定義的結構。 您應該創建一個函數來提取每個子項的值,以便您可以遞歸地重用它。
IEnumerable<string> GetCategoryNames(JObject data)
{
yield return (string)data["name"];
foreach (var name in data["children_data"].Cast<JObject>().SelectMany(GetCategoryNames))
yield return name;
}
然后在根對象上調用它以將您的名字放入列表或其他內容。
var obj = JObject.Parse(response.Content);
var names = GetCategoryNames(obj).ToList();
否則,如果您想不加區分地獲取對象樹中的所有名稱,請記住SelectToken()
/ SelectTokens()
也需要json路徑查詢。 因此,要獲得所有后代的所有名稱,您只需執行以下操作:
let names = obj.SelectTokens("..name").ToList();
如果是我,我會創建我能提出的最完整的JSON文件(包括所有可能的條目),然后使用json2csharp或等效工具來創建c#類,然后反序列化Json並本地使用它。 你可能不得不按摩結果 - 但我認為你可以到達那里。 如果這不起作用,我會使用Newtonsofts JSON LINQ功能(文檔) 。 JToken為您提供父/子組合,允許您在樹上上下移動。 文檔中的示例非常好,因此無需填寫帶有重復信息的頁面。
(根據你的例子生成)
public class ChildrenData
{
public int id { get; set; }
public int parent_id { get; set; }
public string name { get; set; }
public bool is_active { get; set; }
public int position { get; set; }
public int level { get; set; }
public int product_count { get; set; }
public List<object> children_data { get; set; }
}
public class RootObject
{
public int id { get; set; }
public int parent_id { get; set; }
public string name { get; set; }
public bool is_active { get; set; }
public int position { get; set; }
public int level { get; set; }
public int product_count { get; set; }
public List<ChildrenData> children_data { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.