在 C# 中使用 JsonValue 访问 json 键
Access json keys using JsonValue in c#
我使用 Google API 到 return 一个地方,然后我使用 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"]
我使用 Google API 到 return 一个地方,然后我使用 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"]