简体   繁体   English

在 javascript 中的 json 对象数组中获取 JSON 键值对

[英]get JSON key value pair within json object array in javascript

Here is my JSON这是我的 JSON

[  
   {  
      "var5":"item-company-1",
      "asd2":"item-company-1",
      "tie1":"0",
      "cxs1":"481.891px",
      "xcve2":"130.563px"
   },
   {  
      "var5":"item-company-2",
      "asd2":"item-company-2",
      "tie1":"0",
      "cxs1":"481.891px",
      "xcve2":"130.563px"
   },
   {  
      "var5":"item-company-3",
      "asd2":"item-company-3",
      "tie1":"0",
      "cxs1":"481.891px",
      "xcve2":"130.563px"
   }
]

How do I read the key and the value?如何读取键和值? Keep in mind I might not know the Key.请记住,我可能不知道密钥。 I tried using...我尝试使用...

var data = JSON.parse(json);

Object.keys(data).forEach(function(prop) {
  // `prop` is the property name
  // `data[prop]` is the property value
     console.log(prop + " = " + data[prop]);
});

However it just outputs然而它只是输出

0 = [object Object]
1 = [object Object]
2 = [object Object]

EDIT FOR CLARIFICATION编辑澄清

in PHP I get the following output which is what I'm trying to achieve from javascript在 PHP 中,我得到以下输出,这是我试图通过 javascript 实现的

0:
var5 => item-company-1
asd2 => item-company-1
tie1 => 0
cxs1 => 481.891px
xcve2 => 130.563px
1:
var5 => item-company-2
asd2 => item-company-2
tie1 => 0
cxs1 => 481.891px
xcve2 => 130.563px
2:
var5 => item-company-3
asd2 => item-company-3
tie1 => 0
cxs1 => 481.891px
xcve2 => 130.563px

This should work...这应该工作...

data.forEach(function(obj) {
  for (let key in obj) {
    let value = obj[key];
    console.log(`Key: ${key}, Value: ${value}`)
  }
});

In your implementation, Object.keys(data) is evaluating to an array of the numeric keys in the data array.在实施方式中, Object.keys(data)被评估的数据阵列中的数字键的阵列。 So the prop parameter in your callback function is not referring to the keys of the objects in the data array.所以回调函数中的prop参数不是指数据数组中对象的键。

Object.keys() Object.keys()

You can try this:你可以试试这个:

 var data = [ { "var5":"item-company-1", "asd2":"item-company-1", "tie1":"0", "cxs1":"481.891px", "xcve2":"130.563px" }, { "var5":"item-company-2", "asd2":"item-company-2", "tie1":"0", "cxs1":"481.891px", "xcve2":"130.563px" }, { "var5":"item-company-3", "asd2":"item-company-3", "tie1":"0", "cxs1":"481.891px", "xcve2":"130.563px" } ] for(var i=0,item;item=data[i++];){ console.log("==========="+i+"=========") for(var key in item){ console.log(key+":"+item[key]) } }

JSON.parse has a reviver function that lets you view key/value pairs at it parses: JSON.parse有一个 reviver 函数,可以让你在解析时查看键/值对:

 var data = '[{"var5":"item-company-1","asd2":"item-company-1","tie1":"0","cxs1":"481.891px","xcve2":"130.563px"},{"var5":"item-company-2","asd2":"item-company-2","tie1":"0","cxs1":"481.891px","xcve2":"130.563px"},{"var5":"item-company-3","asd2":"item-company-3","tie1":"0","cxs1":"481.891px","xcve2":"130.563px"}]'; JSON.parse(data, function(key, value) { console.log(key, "=>", value); return value; });

To iterate just the keys from the objects, use nested loops:要仅迭代对象中的键,请使用嵌套循环:

 var json = '[{"var5":"item-company-1","asd2":"item-company-1","tie1":"0","cxs1":"481.891px","xcve2":"130.563px"},{"var5":"item-company-2","asd2":"item-company-2","tie1":"0","cxs1":"481.891px","xcve2":"130.563px"},{"var5":"item-company-3","asd2":"item-company-3","tie1":"0","cxs1":"481.891px","xcve2":"130.563px"}]'; var data = JSON.parse(json); for (var i = 0; i < data.length; i++) { console.log(i + ":"); for (var key in data[i]) { console.log(key, "=>", data[i][key]); } }

You actually have array for objects.你实际上有对象的数组。 So Code should look like this所以代码应该是这样的

var data = JSON.parse(json);
for (var key in data) {
    for (var prop in data[key]) {
        console.log(prop + " = " + data[key][prop]);
    }
}

Try this on your code在你的代码上试试这个

 var data = [ { "var5":"item-company-1", "asd2":"item-company-1", "tie1":"0", "cxs1":"481.891px", "xcve2":"130.563px" }, { "var5":"item-company-2", "asd2":"item-company-2", "tie1":"0", "cxs1":"481.891px", "xcve2":"130.563px" }, { "var5":"item-company-3", "asd2":"item-company-3", "tie1":"0", "cxs1":"481.891px", "xcve2":"130.563px" } ]; data.forEach(function(obj,k) { console.log(k+":"); // `prop` is the property name // `data[prop]` is the property value Object.keys(obj).forEach(function(key){ console.log(k+":" +key + " = " + obj[key]); }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM