Json数据通过Javascript读取

时间:2016-04-09 09:30:19

标签: javascript json

我正在尝试通过JavaScript从网站上读取json数据。 Json看起来像这样:

{
  "status" : "success",
  "prices" : [
    {
      "market_hash_name" : "AK-47 | Aquamarine Revenge (Battle-Scarred)",
      "price" : "11.38",
      "created_at" : 1460116654
    },

所以,我得到了代码:

if(pricelist.prices.market_hash_name == itemName) {
var price2 = Math.round(Number(pricelist.prices.market_hash_name[itemName].price) * 1000);

我知道我在这里做错了,有人可以帮帮我吗?

编辑:该功能在此处:

function loadItems() {
    $("#refresh-button").remove();
    $("#loading").addClass("active");
    $.getJSON("" + bot_sids[cur_bot], function(data) {
        if (data.success) {
            var i = 0;
            var ready = true;
            var invIndexes = [];
            for (var index in data.rgInventory) {
                invIndexes[i] = index;
                i++;
            }
            i = 0;
            $.getJSON("", function(pricelist) {
                apricething = pricelist;
                if (pricelist.status) {
                    for (id = 0; id < invIndexes.length; id++) {
                        var index = invIndexes[id];
                        var item = data.rgDescriptions[data.rgInventory[index].classid + "_" + data.rgInventory[index].instanceid];
                        if (item.tradable != 1) {
                            continue;
                        }
                        var itemName = item.market_hash_name;
                        var iconUrl = item.icon_url;
                        console.log(itemName);

                        for(i=0; i<pricelist.prices.length; i++){
                        if (pricelist.prices[i].market_hash_name == itemName) {
                            var price2 = Math.round(Number(pricelist.prices[i].market_hash_name.price) * 1000);
                            console.log(itemName);
                            console.log(price2);
                            if (price2 >= 1) {
                                prices2[itemName] = price2;
                                items[id] = {
                                    name: itemName,
                                    price: price2,
                                    iconurl: iconUrl,
                                    classid: data.rgInventory[index].classid,
                                    id: index,
                                    done: true
                                };
                            } else {
                                items[id] = {
                                    done: true
                                };
                            }
                        } else {
                            items[id] = {
                                name: itemName,
                                price: 0,
                                iconurl: iconUrl,
                                classid: data.rgInventory[index].classid,
                                id: index,
                                done: false
                            };
                        }
                        }
                    }
                    finishedLoading();
                }
            });

2 个答案:

答案 0 :(得分:1)

根据您的数据结构,您需要迭代价格。

var price2;
pricelist.prices.some(function (item) {
    if (item.market_hash_name == itemName) {
        price2 = Math.round(Number(item.price) * 1000);
        return true;
    }
});

答案 1 :(得分:0)

试试这个:

if(pricelist.prices[0].market_hash_name == itemName) {
var price2 = Math.round(Number(pricelist.prices[0].market_hash_name.price) * 1000);