[英]Get array from json using javascript
Server returns me such object, but i need only array ITEMS. 服务器返回我这样的对象,但我只需要数组ITEMS。 How can i get it?
我怎么才能得到它? I tried
array['items']
but the result is undefiend
我试过
array['items']
但结果不undefiend
{
"items": [
{
..
},
{
..
},
{
..
}
],..
,..
}
// JSON string returned from the server
var text = '{"items":[{"name":"itemX" ,"price":15},{"name":"itemY","price":25},{"name":"itemZ","price":20}]}';
// Convert the string returned from the server into a JavaScript object.
var object = JSON.parse(text);
// Accessing a specific property value of an item
console.log(object.items[0].name); //itemX
// Extract 'items' in to a separate array
var itemsArray = object.items;
console.log(itemsArray); //[itemObject, itemObject, itemObject]
If you're getting this as a string: 如果将其作为字符串获取:
var json = JSON.parse(my_json_string)
Then, 然后,
var keys = Object.keys(json);
var values = [];
for(var i = 0; i < keys.length; i++){
values.push(json[keys[i]]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.