2017-05-26 9 views
3

C#とJSON.NETを使用して入れ子になったJSON配列を反復しようとしています。 JSONは、オンラインWebストアのカテゴリを表します。以下はその例です。私の目標は、カテゴリのすべての名前のリストを作成することです。C#ネストした配列から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 



} 

答えて

0

これは再帰的に定義された構造のようです。再帰的に再利用できるように、各子の値を抽出する関数を作成する必要があります。

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(); 
0

はそれを私にして、私は(可能なすべてのエントリを含む)を考え出すことができる最も完全なJSONファイルを作成し、その後、json2csharpを使用または同等のツールを使用してC#クラスを作成し、Jsonを逆シリアル化してネイティブで処理します。あなたは結果を少しマッサージしなければならないかもしれませんが、私はあなたがそこに行くことができると思います。それがうまくいかない場合は、Newtonsofts JSON LINQの機能(documentation)を使用します。 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; } 
} 
関連する問題