javascript - 我应该如何在 for 循环中使用 JavaScript 访问 JSON?

标签 javascript html json for-loop

我在读取 JSON 对象内的数据时有点迷失,这是我拥有的 JSON:

var data = [
          {"mes":{
                  "January":{
                     "Inversion":"1000","Fans":"1020"}
                            }
                 },
           {"mes":{
                 "February":{
                     "Inversion":"15500","Fans":"285"}
                            }
            }
            ]

我希望能够打印如下:

1000年1月20日

二月,15500,285

谢谢!

最佳答案

// initialise variable to build output
var out = []

// loop through the array
for(var i=0;i < data.length; i++){

    // get a reference to the inner object
    var item = data[i].mes

    // loop through the inner object (k is the key name)
    for(k in item){
        // push the built up string onto the output array
        out.push(k+", "+item[k].Inversion+", "+item[k].Fans)
    }
}

// display the output, joining with newlines
console.log(out.join("\n"))

关于javascript - 我应该如何在 for 循环中使用 JavaScript 访问 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215058/

相关文章:

javascript - indexedDB setVersion 请求被阻止?

javascript - 用javascript砍掉未使用的小数

javascript - 这个序列可以用 HTML5 完成吗?

javascript - 如何在 Hyperledger Composer 中匹配查询文件中的字符串?

javascript - 如何模拟背景大小 : cover on <img>?

html - Css:如何格式化和删除左右两侧表格的轮廓边框?

javascript - 选择下拉列表后动态输入字段未正确显示

javascript - 如何从json中删除双引号

json - jQuery AjaxSubmit 在 IE9 上未触发

java - DefaultHttpClient超时设置为10秒但只需要1秒就超时