簡體   English   中英

在C#中使用JsonValue訪問JSON密鑰

[英]Access json keys using JsonValue in c#

我使用Google API返回地點,然后使用JsonValue訪問它。 當我想訪問結果時,一切正常,當我更深入時,我得到超時異常。如何訪問其他對象並通過JSON對象進行迭代?

響應:

{
   "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"];

一個能夠在resultsJson中看到響應,但是當我嘗試使用resultsJson之后直接訪問幾何時:

JsonValue geometryJson = resultsJson["geometry"];

我收到超時異常。 我究竟做錯了什么?

因為results是一個數組,所以您應該嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM