从JSON中提取几个级别的属性

时间:2015-06-02 17:55:24

标签: javascript json properties

代码:

IEnumerator Radar()
{
    radarURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+ "43.761223" + "," + "11.280470" + "&key=" + APIkey;
    WWW googleResp = new WWW(radarURL);
    yield return googleResp;
    googleRespStr =  googleResp.text;
    JSONNode jsonReturn = JSON.Parse(googleResp.text);
    string results = jsonReturn["results"]["formatted_address"].Value;
    Debug.Log(jsonReturn["results"]["formatted_address"].Value);
    textDebug.text = jsonReturn;
    Debug.Log(jsonReturn); 
}

输出JSON:

{


"results" : [
      {
         "address_components" : [
            {
               "long_name" : "45-47",
               "short_name" : "45-47",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Via Bartolomeo Scala",
               "short_name" : "Via Bartolomeo Scala",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Firenze",
               "short_name" : "Firenze",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Firenze",
               "short_name" : "Firenze",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Città Metropolitana di Firenze",
               "short_name" : "FI",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Toscana",
               "short_name" : "Toscana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "50126",
               "short_name" : "50126",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Via Bartolomeo Scala, 45-47, 50126 Firenze, Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 43.7613726,
                  "lng" : 11.2806527
               },
               "southwest" : {
                  "lat" : 43.76119389999999,
                  "lng" : 11.2803103
               }
            },
            "location" : {
               "lat" : 43.76124780000001,
               "lng" : 11.2804451
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 43.76263223029149,
                  "lng" : 11.2818304802915
               },
               "southwest" : {
                  "lat" : 43.7599342697085,
                  "lng" : 11.2791325197085
               }
            }
         },
         "place_id" : "EjJWaWEgQmFydG9sb21lbyBTY2FsYSwgNDUtNDcsIDUwMTI2IEZpcmVuemUsIEl0YWxpYQ",
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Firenze",
               "short_name" : "Firenze",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Firenze",
               "short_name" : "Firenze",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Città Metropolitana di Firenze",
               "short_name" : "FI",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Toscana",
               "short_name" : "Toscana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Firenze, Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 43.8329368,
                  "lng" : 11.3278993
               },
               "southwest" : {
                  "lat" : 43.7269795,
                  "lng" : 11.1540365
               }
            },
            "location" : {
               "lat" : 43.7695604,
               "lng" : 11.2558136
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 43.8329368,
                  "lng" : 11.3278993
               },
               "southwest" : {
                  "lat" : 43.7269795,
                  "lng" : 11.1540365
               }
            }
         },
         "place_id" : "ChIJrdbSgKZWKhMRAyrH7xd51ZM",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "50126",
               "short_name" : "50126",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Firenze",
               "short_name" : "Firenze",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Città Metropolitana di Firenze",
               "short_name" : "FI",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Toscana",
               "short_name" : "Toscana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "50126 Firenze FI, Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 43.7661839,
                  "lng" : 11.321206
               },
               "southwest" : {
                  "lat" : 43.7383011,
                  "lng" : 11.267919
               }
            },
            "location" : {
               "lat" : 43.7592388,
               "lng" : 11.2920619
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 43.7661839,
                  "lng" : 11.321206
               },
               "southwest" : {
                  "lat" : 43.7383011,
                  "lng" : 11.267919
               }
            }
         },
         "place_id" : "ChIJRS-JDZ1TKhMRIO59QpQsCBw",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Firenze",
               "short_name" : "Firenze",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Città Metropolitana di Firenze",
               "short_name" : "FI",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Toscana",
               "short_name" : "Toscana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Firenze FI, Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 43.8352047,
                  "lng" : 11.3391437
               },
               "southwest" : {
                  "lat" : 43.7258401,
                  "lng" : 11.1506007
               }
            },
            "location" : {
               "lat" : 43.7890761,
               "lng" : 11.2297105
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 43.8352047,
                  "lng" : 11.3391437
               },
               "southwest" : {
                  "lat" : 43.7258401,
                  "lng" : 11.1506007
               }
            }
         },
         "place_id" : "ChIJ30yk06BWKhMR2ZdgiWilRbo",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Città Metropolitana di Firenze",
               "short_name" : "FI",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Toscana",
               "short_name" : "Toscana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Città Metropolitana di Firenze, Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 44.239233,
                  "lng" : 11.7529629
               },
               "southwest" : {
                  "lat" : 43.4522844,
                  "lng" : 10.7111173
               }
            },
            "location" : {
               "lat" : 43.7679178,
               "lng" : 11.2523792
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 44.239233,
                  "lng" : 11.7529629
               },
               "southwest" : {
                  "lat" : 43.4522844,
                  "lng" : 10.7111173
               }
            }
         },
         "place_id" : "ChIJY3a9eKD4KhMRYIDk45AsCAM",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Toscana",
               "short_name" : "Toscana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Toscana, Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 44.4726899,
                  "lng" : 12.3713555
               },
               "southwest" : {
                  "lat" : 42.2384378,
                  "lng" : 9.6867213
               }
            },
            "location" : {
               "lat" : 43.7710513,
               "lng" : 11.2486208
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 44.4726899,
                  "lng" : 12.3713555
               },
               "southwest" : {
                  "lat" : 42.23844280000001,
                  "lng" : 9.6867213
               }
            }
         },
         "place_id" : "ChIJezSAEFMr1BIRq1kgW7rDxro",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Italia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 47.092,
                  "lng" : 18.5205015
               },
               "southwest" : {
                  "lat" : 35.4929201,
                  "lng" : 6.6267201
               }
            },
            "location" : {
               "lat" : 41.87194,
               "lng" : 12.56738
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 47.092,
                  "lng" : 18.5205015
               },
               "southwest" : {
                  "lat" : 35.4929201,
                  "lng" : 6.6267201
               }
            }
         },
         "place_id" : "ChIJA9KNRIL-1BIRb15jJFz1LOI",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

我遇到了JSON问题。我想提取" formatted_address" (在这种情况下:" Via Bartolomeo Scala,45-47,50126 Firenze,Italia")。

我试过了:

Debug.Log(jsonReturn["results"]["formatted_address"].Value);

但这不起作用。什么是正确的代码?

1 个答案:

答案 0 :(得分:0)

enter image description here我注意到你的结果是一个数组。您需要访问该数组的第一个元素:

jsonReturn["results"][0]["formatted_address"].Value

资源:http://wiki.unity3d.com/index.php/SimpleJSON