循环通过JSON文件 [英] Looping through a JSON file
问题描述
正如标题所示,我正在尝试阅读如下所示的JSON文件:
As the title says I'm trying to read a JSON file that looks like the following:
{
"response": {
"current_time": 1490040712,
"success": 1,
"items": {
"AK-47 | Black Laminate (Minimal Wear)": {
"last_updated": 1490025658,
"value": 985,
"quantity": 124
},
"AK-47 | Aquamarine Revenge (Field-Tested)": {
"last_updated": 1490025658,
"value": 1775,
"quantity": 127
},
"AK-47 | Black Laminate (Field-Tested)": {
"last_updated": 1490025658,
"value": 890,
"quantity": 130
},
"AK-47 | Aquamarine Revenge (Battle-Scarred)": {
"last_updated": 1490025658,
"value": 1202,
"quantity": 70
},
}
}
}
现在我要做的是遍历JSON文件并检查是否,例如,AK-47 | Aquamarine Revenge(Battle-Scarred)与给定的字符串相同,然后返回它的价值。我试着在互联网上看看这样做但我没有真正变得更聪明,我尝试了几种方法但他们并没有那么顺利。
Now what I'm trying to do is loop through the JSON file and check if, for example, "AK-47 | Aquamarine Revenge (Battle-Scarred)" is the same as a given string, then return it's value. I tried looking on the internet to get a good look of doing this but I didn't really get much smarter, I tried a few ways but they didn't go so well.
我试过的东西:
var d, i;
for (i = 0; i < prices.response.items.length; i++) {
//d = prices.response.data[i];
itemPrice = i;
}
此作品仅供至少返回测试什么,但它根本不起作用,有人可以帮助我吗?
This piece was for testing to just return at least something, but it doesn't work at all, can someone help me?
亲切的问候!
推荐答案
这是检查单个值的方法:
This is how you can check an individual value:
var json = {"response":
{"success":1,
"current_time":1490040712,
"items":
{"AK-47 | Aquamarine Revenge (Battle-Scarred)": {"last_updated":1490025658,"quantity":70,"value":1202},
"AK-47 | Aquamarine Revenge (Factory New)":{"last_updated":1490025658,"quantity":46,"value":3465},
"AK-47 | Aquamarine Revenge (Field-Tested)":{"last_updated":1490025658,"quantity":127,"value":1775},
"AK-47 | Aquamarine Revenge (Minimal Wear)":{"last_updated":1490025658,"quantity":92,"value":2427},
"AK-47 | Aquamarine Revenge (Well-Worn)":{"last_updated":1490025658,"quantity":87,"value":1527},
"AK-47 | Black Laminate (Battle-Scarred)":{"last_updated":1490025658,"quantity":45,"value":955},
"AK-47 | Black Laminate (Factory New)":{"last_updated":1490025658,"quantity":11,"value":10152},
"AK-47 | Black Laminate (Field-Tested)":{"last_updated":1490025658,"quantity":130,"value":890},
"AK-47 | Black Laminate (Minimal Wear)":{"last_updated":1490025658,"quantity":124,"value":985}}
}
};
alert(json.response.items["AK-47 | Aquamarine Revenge (Battle-Scarred)"].value);
由于items不是一个列表,而是一个对象,我们不能简单地遍历它。
你只需要像使用
一样迭代它们这就是你得到属性数量的方法:
Since items is not a list, but an object, we cannot simply iterate through it. You just need to iterate through them like using This is how you get the number of properties:
Object.getOwnPropertyNames(json.response.items).length
这里有一个代码示例,说明如何遍历链接上的键和值。
Here there is a code example of how to iterate through keys and values at the link.
Object.getOwnPropertyNames(json.response.items).forEach(
function (val, idx, array) {
console.log(val + ' -> ' + obj[val]);
}
);
这篇关于循环通过JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!