【问题标题】:Looping through a JSON file循环一个 JSON 文件
【发布时间】:2017-03-20 20:56:19
【问题描述】:

正如标题所说,我正在尝试读取如下所示的 JSON 文件:

{
    "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)”是否与给定字符串相同,然后返回它的值。我尝试在互联网上查看以更好地了解这样做,但我并没有真正变得更聪明,我尝试了几种方法,但效果不佳。

我尝试过的东西:

    var d, i;
    for (i = 0; i < prices.response.items.length; i++) {
        //d = prices.response.data[i];
        itemPrice = i;
    }

这篇文章是为了测试至少返回一些东西,但它根本不起作用,有人可以帮助我吗?

亲切的问候!

【问题讨论】:

  • 您使用什么语言?爪哇? C#? Javascript?
  • 这是做什么的?它所做的是使 itemPrice 等于 price.response.items.length 减一。
  • 请阅读How to Ask。关键词:“搜索和研究”和“解释......任何阻碍你自己解决的困难”。
  • 感谢 cmets 伙计们,我确实解决了。很抱歉没有说明它是什么代码,但这是 Javascript(在 NodeJS 中)。

标签: json loops iteration


【解决方案1】:

这是检查单个值的方法:

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 不是一个列表,而是一个对象,我们不能简单地遍历它。 您只需要像使用一样遍历它们 这是您获取属性数量的方式:

 Object.getOwnPropertyNames(json.response.items).length

见参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames

这里有一个代码示例,说明如何在链接处迭代键和值。

 Object.getOwnPropertyNames(json.response.items).forEach(
   function (val, idx, array) {
    console.log(val + ' -> ' + obj[val]);
   }
 );

【讨论】:

  • 非常感谢!这是我第一次使用 JSON / TXT 文件,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2021-12-09
相关资源
最近更新 更多