Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从Angular 2中的JSON对象中读取所需数据?_Javascript_Angularjs_Json - Fatal编程技术网

Javascript 如何从Angular 2中的JSON对象中读取所需数据?

Javascript 如何从Angular 2中的JSON对象中读取所需数据?,javascript,angularjs,json,Javascript,Angularjs,Json,我在读取和记录JSON文件时遇到如下问题: { "results" : [ { "address_components" : [ { "long_name" : "277", "short_name" : "277", "types" : [ "street_number" ] }, { "long_name" : "Bedford Avenue"

我在读取和记录JSON文件时遇到如下问题:

{
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "277",
           "short_name" : "277",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Bedford Avenue",
           "short_name" : "Bedford Ave",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Williamsburg",
           "short_name" : "Williamsburg",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Brooklyn",
           "short_name" : "Brooklyn",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Kings County",
           "short_name" : "Kings County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "11211",
           "short_name" : "11211",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
     "geometry" : {
        "location" : {
           "lat" : 40.714232,
           "lng" : -73.9612889
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 40.7155809802915,
              "lng" : -73.9599399197085
           },
           "southwest" : {
              "lat" : 40.7128830197085,
              "lng" : -73.96263788029151
           }
        }
     },
     "place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
     "types" : [ "street_address" ]
  },
  ],
  "status" : "OK"
  }
我只需要城市的名字,不是所有的数据。我怎么能只记录想要的对象行

我尝试了这段代码,但效果不理想:

Object.keys(JSON[0]);
对于您的示例数据

>> data['results'][0]['address_components'][2]['long_name']
Williamsburg

>> data['results'][0]['address_components'][3]['long_name']
Brooklyn
如果以JSON字符串的形式获取数据,则需要先将其转换为JavaScript对象

data = JSON.parse(the_json_string_here);
对于您的示例数据

>> data['results'][0]['address_components'][2]['long_name']
Williamsburg

>> data['results'][0]['address_components'][3]['long_name']
Brooklyn
如果以JSON字符串的形式获取数据,则需要先将其转换为JavaScript对象

data = JSON.parse(the_json_string_here);

您可以通过
sublocality\u level\u 1
进行筛选,并在函数中执行此操作,以便在收到类似的JSON排序结果时重用它

const getCity = data => data.address_components
      .filter(x => x.types && x.types.indexOf("sublocality_level_1") > -1)
      .map(x => x.long_name)[0];

//Get a city name from only the first result 
const oneCity = getCity(jsonData.results[0]);

//Get an array with all cities (for all the results)
const allCities = jsonData.results.map(getCity);

您可以通过
sublocality\u level\u 1
进行筛选,并在函数中执行此操作,以便在收到类似的JSON排序结果时重用它

const getCity = data => data.address_components
      .filter(x => x.types && x.types.indexOf("sublocality_level_1") > -1)
      .map(x => x.long_name)[0];

//Get a city name from only the first result 
const oneCity = getCity(jsonData.results[0]);

//Get an array with all cities (for all the results)
const allCities = jsonData.results.map(getCity);
您想要“美国纽约州布鲁克林贝德福德大街277号,邮编11211”还是“布鲁克林”?数据中的“城市”是什么成分?您想要“美国纽约州布鲁克林贝德福德大街277号,邮编11211”还是“布鲁克林”?数据中的“城市”是什么成分?