2016-04-25 22 views
0

如何在此Json中使用foreach,我嘗試使用多個foreach,但我無法做到,我只想檢索座標數據。Foreach大JSON

目前這是我的代碼來檢索數據和此代碼不起作用,我得到一個錯誤:(Newtonsoft.Json.Linq.JProperty'不包含'座標'的定義)。

String toto = await response.Content.ReadAsStringAsync(); 
dynamic jsonn = JValue.Parse(toto); 
dynamic deserializedValue = JsonConvert.DeserializeObject(toto); 


       foreach (var data in deserializedValue.resourceSets) 
       { 
        foreach (var data1 in data.resources) 
        { 
         foreach (var data2 in data1.point) 
         { 
          foreach (var data3 in data2.coordinates) 
          { 
           var message = new MessageDialog("Coord: " + data3.coordinates); 
           await message.ShowAsync(); 
          } 

         } 
        } 
       } 
      } 

{ 
    "authenticationResultCode": "ValidCredentials", 
    "brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png", 
    "copyright": "Copyright © 2016 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.", 
    "resourceSets": [ 
    { 
     "estimatedTotal": 1, 
     "resources": [ 
     { 
      "__type": "Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1", 
      "bbox": [ 
      41.298164367675781, 
      -5.2461638450622559, 
      51.099018096923828, 
      9.6006984710693359 
      ], 
      "name": "France", 
      "point": { 
      "type": "Point", 
      "coordinates": [ 
       46.637279510498047, 
       2.3382623195648193 
      ] 
      }, 
      "address": { 
      "countryRegion": "France", 
      "formattedAddress": "France" 
      }, 
      "confidence": "High", 
      "entityType": "CountryRegion", 
      "geocodePoints": [ 
      { 
       "type": "Point", 
       "coordinates": [ 
       46.637279510498047, 
       2.3382623195648193 
       ], 
       "calculationMethod": "Rooftop", 
       "usageTypes": [ 
       "Display" 
       ] 
      } 
      ], 
      "matchCodes": [ 
      "Good" 
      ] 
     } 
     ] 
    } 
    ], 
    "statusCode": 200, 
    "statusDescription": "OK", 
    "traceId": "80d14996537e49218145ff171faeca00|BN20130533|02.00.164.1500|BN2SCH020171462, i-42be63df.us-east-1b, i-e3280660.us-east-1b, BN2SCH020201258" 
} 
+0

我喜歡「動態」的可用性,但這對我來說是一種代碼味道。我很難找到許多用於動態的用途,除了客戶端之外,我發現它非常方便;但通常將我的用途限制在該模式。爲了以這種方式處理對象,我可能會建議更具體的模式來表示節點。在編譯之前,Intellisense會挑選你的問題。 – Hardrada

回答

0

point是一個對象,而不是一個數組。

"point": {  // <- Notice the '{' 
     "type": "Point", 
     "coordinates": [ 
      46.637279510498047, 
      2.3382623195648193 
     ] 
     }, 

這裏是你如何閱讀:

foreach (var set in deserializedValue.resourceSets) 
{ 
    foreach (var resource in set.resources) 
    { 
     foreach (var coord in resource.point.coordinates) 
     { 
      var message = new MessageDialog("Coord: " + coord); 
      await message.ShowAsync(); 
     } 
    } 
} 
0

看結構:

{ <---layer #1 
    "authenticationResultCode": "ValidCredentials", 
    "brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png", 
    "copyright": "Copyright ...", 
    "resourceSets": [ <-- layer #2 
     { <-- layer #3 
      "estimatedTotal": 1, 
      "resources": [ <-- layer #4 
       { <-- layer #5 
         "__type": "Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1", 
         "bbox": [ 
          41.298164367675781, 
          -5.2461638450622559, 
          51.099018096923828, 
          9.6006984710693359 
         ], 
         "name": "France", 
         "point": { <--layer #6 
         "type": "Point", 
         "coordinates": [ 

coordinates是6層下來,你有4層未築巢夜代碼。

0

謝謝您的回答,現在我應該怎麼座標轉換成字符串?