如何使用javascript访问json对象的元素?

如何使用javascript访问json对象的元素?,javascript,Javascript,数据如下所示,由Flask应用程序中的Python脚本生成: {"took": 0, "timed_out": false, "_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}, "hits": {"total": {"value": 3775, "relation": "eq"}, "max_score": 3.0824933, "hits": [{"_index": "ab", "_type": "_doc"

数据如下所示,由Flask应用程序中的Python脚本生成:

{"took": 0, "timed_out": false, "_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}, "hits": {"total": {"value": 3775, "relation": "eq"}, "max_score": 3.0824933, "hits": [{"_index": "ab", "_type": "_doc", "_id": "amjDgHABpFPexwJkaQM0", "_score": 3.0824933, "_source": {"reviews_per_month": "", "price": "150", "number_of_reviews": "0", "last_review": "", "host_id": "4632", "availability_365": "365", "id": "3647", "neighbourhood": "Harlem", "calculated_host_listings_count": "1", "neighbourhood_group": "Manhattan", "name": "THE VILLAGE OF HARLEM....NEW YORK !", "minimum_nights": "3", "longitude": "-73.9419", "host_name": "Elisabeth", "latitude": "40.80902", "room_type": "Private room"}}, {"_index": "ab", "_type": "_doc", "_id": "hmjDgHABpFPexwJkaQM0", "_score": 3.0824933, "_source": {"reviews_per_month": "2.04", "price": "50", "number_of_reviews": "242", "last_review": "01/06/2019", "host_id": "32294", "availability_365": "355", "id": "9668", "neighbourhood": "Harlem", "calculated_host_listings_count": "3", "neighbourhood_group": "Manhattan", "name": "front room/double bed", "minimum_nights": "3", "longitude": "-73.95104", "host_name": "Ssameer Or Trip", "latitude": "40.82245", "room_type": "Private room"}}, {"_index": "ab", "_type": "_doc", "_id": "h2jDgHABpFPexwJkaQM0", "_score": 3.0824933, "_source": {"reviews_per_month": "1.42", "price": "52", "number_of_reviews": "88", "last_review": "14/06/2019", "host_id": "32045", "availability_365": "255", "id": "9704", "neighbourhood": "Harlem", "calculated_host_listings_count": "1", "neighbourhood_group": "Manhattan", "name": "Spacious 1 bedroom in luxe building", "minimum_nights": "2", "longitude": "-73.95466", "host_name": "Teri", "latitude": "40.81305", "room_type": "Private room"}}]}}
我想使用JavaScript将这些数据的元素打印到网页上的HTML表中。JavaScript代码片段是:

function test() {
fetch('https://exxxxh.azurewebsites.net/search') 
.then(response => response.json())
.then(data => {
  console.log(data['hits']['hits'][0]['_source']['host_name']) 
  for (var i = 0; i < data.length; i++) {
    console.log(data[i]['hits']['hits'][0]['_source']['host_name'])
  }
})
.catch(error => console.error(error))
}
功能测试(){
取('https://exxxxh.azurewebsites.net/search') 
.then(response=>response.json())
。然后(数据=>{
console.log(数据['hits']['hits'][0]['u source']['host\u name']))
对于(变量i=0;iconsole.error(错误))
}
在for循环之前,控制台打印“Elisabeth”——数据中的第一个主机名。for循环不工作,控制台日志中没有打印任何内容。如何遍历数据并提取所有主机名?当然,在那之后,还有其他因素。
多谢各位

如果要使用for循环,可以这样做

for (let i = 0; i < input.hits.hits.length; i++) {
  console.log(input.hits.hits[i]._source.host_name);
}

数据['hits']是一个对象,而不是数组。 数据['hits']['hits']是一个数组

试试这个:

var hitsArray = data['hits']['hits'];
for (var i = 0; i < hitsArray.length; i++) {
  console.log(hitsArray[i]['_source']['host_name'])
}
var hitsArray=data['hits']['hits'];
对于(var i=0;i
如果我理解正确,您希望取出input.hits.hits数组中每个主机名的值。 尝试:

结果:

[ 'Elisabeth', 'Ssameer Or Trip', 'Teri' ]

希望这有帮助。

数据是否有
长度属性?这是一个很好的问题。我想是的,但我不知道。实际上,长度是3,因为这是Python应用程序中搜索脚本的限制。也许我们应该在这个基础上工作。谢谢。所有的作品,都以不同的方式设计,但这正是我想要的。
[ 'Elisabeth', 'Ssameer Or Trip', 'Teri' ]