2016-04-09 68 views
-2

我想通过JavaScript从网站读取json数据。 JSON看起来是这样的:通过Javascript读取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(); 
       } 
      }); 
+0

是什么问题?错误?您想做什么? –

+0

它不适合我,它没有达到那个价格2。如果它会到达那里,我会去:console.log(price2); –

+0

您能否将此减少到20-30行代码,以便我们可以看到发生了什么? –

回答

0

试试这个:

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

谢谢,但没有奏效。只是说,那只是JSON的“小”部分。这实际上要大得多。如果需要,我可以将所有内容发布到pastebin。 –

+0

你可以尝试使用循环。 –

0

您的调音有些东西在这里。 prices是一个数组,但您不访问数组的元素,而是访问整个数组。

然后在数字计算中,您试图以数组的形式访问market_hash_name,这是一个字符串。

因此,如果名称匹配,以下代码将遍历您的prices并计算price2

for(var i=0; i<pricelist.prices.length; i++){ 
    if (pricelist.prices[i].market_hash_name === itemName) { 
     var price2 = Math.round(Number(pricelist.prices[i].price) * 1000); 
    } 
} 
+0

嗯,我会编辑我的帖子。所以你可以看到“完整的代码”。此外,它似乎工作,但事情是。价格他得到了什么“NaN”。 –

+0

它适合我。你可以在这里尝试:https://jsfiddle.net/kpk55nww/ –

+0

在你的pastebin中,你仍然有价格计算中的'market_hash_name' –

0

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

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