2015-04-19 83 views
0

我在從Steam API(http://steamcommunity.com/dev)中檢索到的JSON文件內部獲取值時遇到了問題。從JSON獲取值,Node.js

的問題,當我試圖打印數據時,Node.js的文件代碼如下:

var data = JSON.parse(body); // Stores the JSON data which has been retrieved 
console.log(data.result.toString(350462890).market_hash_name); // Attempts to grab the value of the market_hash_name from the JSON data and display it to screen 

我得到如下回應:「未定義」。下面使用

JSON數據:

{ 
"result": { 
    "350462890": { 
     "icon_url": "fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5-OOqhNQh0fTvSAK5KVPAoywXpDS4n5YliBtazruNQfgrssNfPN-IqYtkdSpTZU_OCYAir70luiaAPfZOIqHznw223bZvDH3kW", 
     "icon_url_large": "fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5-OOqhNQh0fTvSAK5KVPAoywXpDS4n5fhvVcWx8vUHe126vYrANLYvNI1FG5LWCPfXM1304048hqALKpffqSu9jyvoMjgCRVO1rexMsCC1", 
     "icon_drag_url": "", 
     "name": "Dual Berettas | Panther", 
     "market_hash_name": "Dual Berettas | Panther (Field-Tested)", 
     "market_name": "Dual Berettas | Panther (Field-Tested)", 
     "name_color": "D2D2D2", 
     "background_color": "", 
     "type": "Mil-Spec Grade Pistol", 
     "tradable": "1", 
     "marketable": "1", 
     "commodity": "0", 
     "fraudwarnings": "", 
     "descriptions": { 
      "0": { 
       "type": "html", 
       "value": "Exterior: Field-Tested", 
       "app_data": "" 
      }, 
      "1": { 
       "type": "html", 
       "value": " ", 
       "app_data": "" 
      }, 
      "2": { 
       "type": "html", 
       "value": "Firing two large-mag Berettas at once will lower accuracy and increase load times. On the bright side, you'll get to fire two large-mag Berettas at once. It has been painted in a black, grey and red color scheme.", 
       "app_data": "" 
      }, 
      "3": { 
       "type": "html", 
       "value": " ", 
       "app_data": "" 
      }, 
      "4": { 
       "type": "html", 
       "value": "The Arms Deal 3 Collection", 
       "color": "9da1a9", 
       "app_data": { 
        "def_index": "65535", 
        "is_itemset_name": "1" 
       } 
      }, 
      "5": { 
       "type": "html", 
       "value": " ", 
       "app_data": "" 
      } 
     }, 
     "owner_descriptions": "", 
     "actions": { 
      "0": { 
       "name": "Inspect in Game...", 
       "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D14429613083935122456" 
      } 
     }, 
     "market_actions": { 
      "0": { 
       "name": "Inspect in Game...", 
       "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D14429613083935122456" 
      } 
     }, 
     "tags": { 
      "0": { 
       "internal_name": "CSGO_Type_Pistol", 
       "name": "Pistol", 
       "category": "Type", 
       "category_name": "Type" 
      }, 
      "1": { 
       "internal_name": "weapon_elite", 
       "name": "Dual Berettas", 
       "category": "Weapon", 
       "category_name": "Weapon" 
      }, 
      "2": { 
       "internal_name": "set_weapons_iii", 
       "name": "The Arms Deal 3 Collection", 
       "category": "ItemSet", 
       "category_name": "Collection" 
      }, 
      "3": { 
       "internal_name": "normal", 
       "name": "Normal", 
       "category": "Quality", 
       "category_name": "Category" 
      }, 
      "4": { 
       "internal_name": "Rarity_Rare_Weapon", 
       "name": "Mil-Spec Grade", 
       "category": "Rarity", 
       "color": "4b69ff", 
       "category_name": "Quality" 
      }, 
      "5": { 
       "internal_name": "WearCategory2", 
       "name": "Field-Tested", 
       "category": "Exterior", 
       "category_name": "Exterior" 
      } 
     }, 
     "classid": "350462890" 
    }, 
    "success": true 
} 

}

因此,沒有人有任何想法,我怎麼可以返回market_hash_name?也請注意,我對使用Node.js相當陌生。

+0

其中顯示了一個貧窮的想法是用數字標識符 – mplungjan

+0

的可能重複[解析JSON在JavaScript?](http://stackoverflow.com/questions/4935632/parse-json-in-javascript) – Abhishek

回答

5
data.result['350462890'].market_hash_name 
+0

請注意,您不需要將數字轉換爲字符串。傳遞一個數字也可以 – slebetman