gpt4 book ai didi

javascript - 使用 javascript 从 json 获取数组

转载 作者:行者123 更新时间:2023-12-03 10:23:50 25 4
gpt4 key购买 nike

服务器返回给我这样的对象,但我只需要数组ITEMS。我怎么才能得到它?我尝试了 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]

关于javascript - 使用 javascript 从 json 获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498053/

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