2016-04-25 6 views
0

Wie kann ich foreach in diesem Json verwenden, ich versuche mit mehreren foreach, aber ich kann es nicht tun, ich möchte nur die Koordinaten Daten abrufen.Foreach eine große JSON

Im Moment ist dies mein Code zum Abrufen der Daten und dieser Code funktioniert nicht, ich bekomme einen Fehler: (Newtonsoft.Json.Linq.JProperty 'enthält keine Definition für' Koordinaten ').

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

Ich liebe die Verfügbarkeit von "dynamisch", aber das ist ein Code-Geruch für mich. Ich bin schwer zu finden, viele Anwendungen für dynamische EXCEPT für den Weg bis zum Client, wo ich finde, es ist sehr praktisch; aber in der Regel beschränke ich mich auf dieses Muster. Um auf diese Weise mit Objekten zu arbeiten, könnte ich ein konkreteres Schema zur Darstellung Ihrer Knoten vorschlagen. Intellisense hätte Ihr Problem aufgegriffen, bevor die Kompilation überhaupt möglich war. – Hardrada

Antwort

0

point ist ein Objekt, kein Array.

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

Hier ist, wie Sie es lesen:

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

Blick auf die Struktur:

{ <---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 Schichten nach unten, und Sie haben in yoru Code 4 Schichten un-Verschachtelung bekam.

0

Vielen Dank für Ihre Antwort, wie ich jetzt coord in einen String umwandeln sollte?