简体   繁体   中英

Access json keys using JsonValue in c#

I' using Google API to return a place and then I access it using JsonValue. Everything works when I want to access results, when I go 'deeper" I get timeout exceptions. How to access other objects and iterate through a JSON object?

The response:

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.867551,
               "lng" : 151.200817
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "b0277cade7696e575824681aba949d68814f9efe",
         "name" : "Sydney New Year's Eve Cruises",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 813,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/107666140764925298472/photos\"\u003eSydney New Year's Eve Cruises\u003c/a\u003e"
               ],
               "photo_reference" : "CoQBcwAAAF5NycTw0r7cw8CHxZCKNvFGUSFndKXuPT4j5lXZXI_T-1SkRfdGwhayhQdRD0-4omka8cFZc02E8grQ7t8LduyznGgIBCocN24QLznNY2q9x3FmW-d-Ry74CNo3WX0YQKXg2JqIbXRH86X-X_TXGNrc75_fQwd-x8CE2-MeaVn3EhD1hfBnjPtXGv9QS234kiOIGhStf_EjMEL53bwDKocQ5cw3iYhCUA",
               "width" : 1300
            }
         ],
         "place_id" : "ChIJ__8_hziuEmsR27ucFXECfOg",
         "reference" : "CoQBcQAAAEW7axO9F7XCp3AMXS6VjAOYY4g-pzVSmvZmC3kARg7wHywP_jI4tbNSD01q0hzdqxfe9fHxruuNeaaM-5VWKiNumQuFBKYAitfKlUGB8BxQlnJ3jef_7hVgquryh4Vm2Qo9EOl9-BLlpdHt0tdMDQhZHK3XKVMf024gWYP3F1sxEhBVaaVAe3r51tlCIsn_-z48GhQiawR_3Sh9KQOJ2wNqjoh42FazBA",
         "scope" : "GOOGLE",
         "types" : [
            "travel_agency",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "32 The Promenade, King Street Wharf 5, Sydney Nsw 2000, Sydney"
      }
   ],
   "status" : "OK"
}

C#:

JsonValue json = await FetchDataAsync (url);
JsonValue resultsJson = json["results"];

A am able to see the response in resultsJson, but when I try to access geometry straight after resultsJson using:

JsonValue geometryJson = resultsJson["geometry"];

I get timeout exceptions. What am I doing wrong?

因为results是一个数组,所以您应该尝试这样的事情:

json["results"][0]["geometry"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM