Looping through a JSON file

2019-09-15 02:40发布

问题:

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
            }, 
        }
    }
}

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.

Something I tried:

    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?

Kind regards!

回答1:

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);

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

See reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames

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]);
   }
 );