从多个数组中打印json值

时间:2014-07-08 12:57:20

标签: javascript json

请帮帮我。我需要得到

"long_name" : "Coimbatore", 
"long_name" : "Tamil Nadu", 
"long_name" : "India", 

使用JavaScript从json数组。否则,请告诉我如何使用javascript地理位置获取地点详细信息。

我的json数组是

{
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "25",
           "short_name" : "25",
           "types" : [ "premise" ]
        },
        {
           "long_name" : "New Siddhapudur",
           "short_name" : "New Siddhapudur",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "641044",
           "short_name" : "641044",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "25, New Siddhapudur, Coimbatore, Tamil Nadu 641044, India",
     "geometry" : {
        "location" : {
           "lat" : 11.018246,
           "lng" : 76.97244200000002
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 11.0195949802915,
              "lng" : 76.97379098029153
           },
           "southwest" : {
              "lat" : 11.0168970197085,
              "lng" : 76.97109301970852
           }
        }
     },
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "New Siddhapudur",
           "short_name" : "New Siddhapudur",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "New Siddhapudur, Coimbatore, Tamil Nadu, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 11.026501,
              "lng" : 76.983266
           },
           "southwest" : {
              "lat" : 11.0161269,
              "lng" : 76.96872499999999
           }
        },
        "location" : {
           "lat" : 11.0211533,
           "lng" : 76.9743873
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 11.026501,
              "lng" : 76.983266
           },
           "southwest" : {
              "lat" : 11.0161269,
              "lng" : 76.96872499999999
           }
        }
     },
     "types" : [ "sublocality_level_1", "sublocality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "641044",
           "short_name" : "641044",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Coimbatore, Tamil Nadu 641044, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 11.0264772,
              "lng" : 76.99028059999999
           },
           "southwest" : {
              "lat" : 11.0101754,
              "lng" : 76.9676549
           }
        },
        "location" : {
           "lat" : 11.0174388,
           "lng" : 76.9844677
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 11.0264772,
              "lng" : 76.99028059999999
           },
           "southwest" : {
              "lat" : 11.0101754,
              "lng" : 76.9676549
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Coimbatore, Tamil Nadu, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 11.1929685,
              "lng" : 77.113359
           },
           "southwest" : {
              "lat" : 10.8914259,
              "lng" : 76.8596541
           }
        },
        "location" : {
           "lat" : 11.0168445,
           "lng" : 76.95583209999999
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 11.1929685,
              "lng" : 77.113359
           },
           "southwest" : {
              "lat" : 10.8914259,
              "lng" : 76.8596541
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Coimbatore, Tamil Nadu, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 11.4057969,
              "lng" : 77.1766741
           },
           "southwest" : {
              "lat" : 10.21881,
              "lng" : 76.656036
           }
        },
        "location" : {
           "lat" : 10.9675289,
           "lng" : 76.91820969999999
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 11.4057969,
              "lng" : 77.1766741
           },
           "southwest" : {
              "lat" : 10.21881,
              "lng" : 76.656036
           }
        }
     },
     "types" : [ "administrative_area_level_2", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Tamil Nadu, India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 13.496666,
              "lng" : 80.3464511
           },
           "southwest" : {
              "lat" : 8.077270199999999,
              "lng" : 76.23055409999999
           }
        },
        "location" : {
           "lat" : 11.1271225,
           "lng" : 78.6568942
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 13.496666,
              "lng" : 80.3464511
           },
           "southwest" : {
              "lat" : 8.077270199999999,
              "lng" : 76.23055409999999
           }
        }
     },
     "types" : [ "administrative_area_level_1", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "India",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 35.5044752,
              "lng" : 97.395555
           },
           "southwest" : {
              "lat" : 6.7535159,
              "lng" : 68.162386
           }
        },
        "location" : {
           "lat" : 20.593684,
           "lng" : 78.96288
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 35.5044752,
              "lng" : 97.395555
           },
           "southwest" : {
              "lat" : 6.7535159,
              "lng" : 68.16288519999999
           }
        }
     },
     "types" : [ "country", "political" ]
  }
],
"status" : "OK"
}

2 个答案:

答案 0 :(得分:0)

假设你想进入长队名单:

首先,查看address_components列表:

var list = obj.results[0].address_components;

使用map创建一个长名数组:

var longnames = list.map(function (el) {
  return el.long_name;
});

然后,您可以使用普通元素访问来获取所需的名称。

longnames[1] // "New Siddhapudur"

DEMO

答案 1 :(得分:0)

民间,

我已经编写了以下代码可以帮助你,结果将在新的json中,并且你已经保存在你的json in obj

var newjson={};
filters=obj.results;for (i in filters){
    //console.log(i);
    for (key in filters[i]){
//        console.log( key + ": " + filters[i][key].length);
         for (var n=0; n<filters[i][key].length;n++){
            if(filters[i][key][n].long_name!=undefined)
            {
            newjson[filters[i][key][n].long_name]=filters[i][key][n].long_name;
            console.log( filters[i][key][n].long_name + ": " + filters[i][key][n].long_name);
            }

         }   
    }
}