我是初学者,请帮助我从下面的数组json对象中获取价值。
[{"Category":"HI","Sub Category":"AQ HIOP"},
{"Category":"2HJ","Sub Category":"AS HIOP"},
{"Category":"3HJ","Sub Category":"AT HIOP"},
{"Category":"4Hj","Sub Category":"AP HIOP"},
{"Category":"5HJ","Sub Category":"AN HIOP"},
]
我只需要Sub Category的值
谢谢。
答案 0 :(得分:1)
您可以通过多种方式进行操作。如果您的对象包含在变量中,则可以这样访问:
var test = [{"Category":"HI","Sub Category":"AQ HIOP"},
{"Category":"2HJ","Sub Category":"AS HIOP"},
{"Category":"3HJ","Sub Category":"AT HIOP"},
{"Category":"4Hj","Sub Category":"AP HIOP"},
{"Category":"5HJ","Sub Category":"AN HIOP"},
]
test[0]["Sub Category"] //Take into account that you have an array
如果密钥中没有空格,可以这样访问:
test[0].Category;
如果您的JSON是字符串,则必须先使用JSON.parse。
这里您将如何插入新数组
var another_array = []
another_array.push({"New category": test[0]["Sub Category"]})
答案 1 :(得分:0)
尝试一下:
var arr = [{"Category":"HI","Sub Category":"AQ HIOP"},
{"Category":"2HJ","Sub Category":"AS HIOP"},
{"Category":"3HJ","Sub Category":"AT HIOP"},
{"Category":"4Hj","Sub Category":"AP HIOP"},
{"Category":"5HJ","Sub Category":"AN HIOP"},
]
for (var i = 0; i < arr.length; i++) {
for (key in arr[i]) {
console.log('Key: '+ key + ' Value: ' + arr[i][key]);
}
}
您可以使用for
和for in
访问您的Objet
答案 2 :(得分:0)
var test = [
{"Category":"HI","Sub Category":"AQ HIOP"},
{"Category":"2HJ","Sub Category":"AS HIOP"},
{"Category":"3HJ","Sub Category":"AT HIOP"},
{"Category":"4Hj","Sub Category":"AP HIOP"},
{"Category":"5HJ","Sub Category":"AN HIOP"},
];
test.forEach(function(item){
console.log(item);
})