gpt4 book ai didi

javascript - 如何使用json数据进行循环?

转载 作者:行者123 更新时间:2023-11-30 10:06:49 26 4
gpt4 key购买 nike

我正在尝试用循环显示 json 数据。

我得到以下结果,但不知道如何用循环显示它们。

[
{"latitude":"23.046100780353495","longitude":"72.56860542227514"},
{"latitude":"23.088427701737665","longitude":"72.49273109366186"},
{"latitude":"23.061264193197644","longitude":"72.68224525381811"},
{"latitude":"22.977212139977677","longitude":"72.52191352774389"},
{"latitude":"23.002180435752084","longitude":"72.47590827872045"},
{"latitude":"23.108638843843046","longitude":"72.49444770743139"}
]

我创建了这个循环:

var obj = JSON.parse(data);
var total = obj.length;

for (var i = 0; i < total; i++) {

console.log([i]);

}

并尝试像这样显示结果:

23.046100780353495,72.56860542227514
23.088427701737665,72.49273109366186
23.061264193197644,72.68224525381811
22.977212139977677,72.52191352774389
23.002180435752084,72.47590827872045
23.108638843843046,72.49444770743139

最佳答案

一些提示:

  • {} 括号是对象,您可以访问 object.property 等属性。
  • [] 括号是数组,您可以通过索引array[index]访问元素。

在您的特定情况下,应该这样做:

var objects_array = JSON.parse(data); // This is an array of objects.
var total = objects_array.length;

for (var i = 0; i < total; i++) {
var obj = objects_array[i]; // This is one object from the array.
console.log( obj.latitude + ',' + obj.longitude ); // We access object properties using a dot.
}

关于javascript - 如何使用json数据进行循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28636774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com