2016-09-20 20 views
1

以下のJSONを解析し、すべての値をscreenshot.thumbnailUrlにしたいと思います。しかし、私の制約は以下の通りです。キーを知らずにjsonを読む#

すべてのノードがスクリーンショットを持っているわけではありません。この例では、「天気」と「エンティティ」だけがそれを持っています。

ノード名がわかりません。この例では、「天気」または「エンティティ」という名前のノードがあることはわかりませんでした。これらのノードは、jsonを取得するために作成したクエリに基づいて自動的に生成されます。

スクリーンショットが存在する可能性がある場所は2つあります。 (1)ルートオブジェクトの子に。 weather.screenshot(2)rootobjectの子のすべての値。 entities.value [0] .screenshotを、entities.value [1] .screenshotなど

{ "_type": "SearchResponse", "queryContext": {}, "webPages": {}, "entities": { 
    "queryScenario": "DominantEntity", 
    "value": [ 
     { 
     "_type": "Place", 
     "id": "https://www.someapi.com/api/v6/#Entities.0", 
     "screenshot": { 
      "thumbnailUrl": "http://Screenshot_URL_I_Want", 
      "width": 285 
     }, 
     "name": "Seattle", 
     "entityPresentationInfo": {}, 
     "bingId": "5fbba6b8-85e1-4d41-9444-d9055436e473", 
     "boundingBox": [], 
     "weather": {}, 
     "timeZone": "Pacific Standard Time" 
     } 
    ] }, "images": {}, "weather": { 
    "id": "https://www.someapi.com/api/v6/#Weather", 
    "screenshot": { 
     "thumbnailUrl": "http://Screenshot_URL_I_Want", 
     "width": 285 
    }, 
    "location": {}, 
    "currentWeather": {}, 
    "dailyForecast": [] }, "rankingResponse": {} } 
+0

デシリアライズ用にjson .netを使用できますか? – StellaMaris

答えて

2

あなたはJSONを解析し、再帰的にそれを反復処理するために、このようなコードを使用することができます。あなたは、おそらくJSONのあなたのタイプのために、それが正しいと堅牢にするためにRecursiveDescentへの呼び出しのラムダにロジックを改良する必要があります:

using Newtonsoft.Json.Linq; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string json = @"....your JSON ...."; 

      var node = JToken.Parse(json); 

      RecursiveDescent(node, n => 
      { 
       JToken url = n["thumbnailUrl"]; 
       if (url != null && url.Type == JTokenType.String) 
       { 
        var nodeWeWant = url?.Parent?.Parent?.Parent?.Parent; 

        Console.WriteLine(nodeWeWant.ToString()); 
       } 
      }); 
     } 

     static void RecursiveDescent(JToken node, Action<JObject> action) 
     { 
      if (node.Type == JTokenType.Object) 
      { 
       action((JObject)node); 
       foreach (JProperty child in node.Children<JProperty>()) 
        RecursiveDescent(child.Value, action); 
      } 
      else if (node.Type == JTokenType.Array) 
      { 
       foreach (JToken child in node.Children()) 
        RecursiveDescent(child, action); 
      } 
     } 
    } 
} 
+0

ありがとう!これは面白い。私はそれを行う簡単な方法があったと思う:) –

2

これは私のために働いていたものです...私はクリーナーを探していますソリューションですが...

static async void getJobject(string jsonstring) 
     { 
      JObject response = await JObject.Parse(jsonstring); 
      foreach (var node in response.Children()) 
      { 
       Console.WriteLine(node.Path); 
       string propertyPath = node.Path + ".screenshot.thumbnailUrl"; 
       var token = response.SelectToken(propertyPath); 

       if (token != null) 
       { 
        Console.WriteLine("Check this=> " + token.ToString()); //Prints screenshot URL from weather 
       } 
       else 
       { 
        propertyPath = node.Path + ".value"; 
        token = response.SelectToken(propertyPath); 
        if (token != null) 
        { 
         int count = token.Children().Count(); 
         for (int i = 0; i < count; i++) 
         { 
          propertyPath = node.Path + ".value" + "[" + i.ToString() + "]" + ".screenshot.thumbnailUrl"; 
          var mytoken = response.SelectToken(propertyPath); 
          if (mytoken != null) 
          { 
           Console.WriteLine("Check this=> " + mytoken.ToString()); //Prints screenshot URL from entities 
          } 
         } 


        } 
       } 

      } 
     } 
関連する問題