Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JSON对象(Steam Api)获取特定数据_Json_Node.js_Steam Web Api - Fatal编程技术网

从JSON对象(Steam Api)获取特定数据

从JSON对象(Steam Api)获取特定数据,json,node.js,steam-web-api,Json,Node.js,Steam Web Api,因此,我正在练习一些nodej,这次我正在使用steamapi和json对象。但是我有一些问题 所以,从蒸汽api来看 http://steamcommunity.com/profiles/<steamid>/inventory/json/730/2 给我这个[{},{},{},{},{},{},{},{},{},{}, {},…] JSON看起来像这样 '1293508920_0': { appid: '730', classid: '1293508920',

因此,我正在练习一些nodej,这次我正在使用steamapi和json对象。但是我有一些问题

所以,从蒸汽api来看

http://steamcommunity.com/profiles/<steamid>/inventory/json/730/2
给我这个<代码>[{},{},{},{},{},{},{},{},{},{}, {},…]

JSON看起来像这样

'1293508920_0': 
   { appid: '730',
     classid: '1293508920',
     instanceid: '0',
     icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FF4u1qubIW4Su4mzxYHbzqGtZ-KGlz8EuJcg3rnE9NiijVe3_UY-Zzr2JJjVLFEEeiQRtg',
     icon_drag_url: '',
     name: 'Shadow Case',
     market_hash_name: 'Shadow Case',
     market_name: 'Shadow Case',
     name_color: 'D2D2D2',
     background_color: '',
     type: 'Base Grade Container',
     tradable: 1,
     marketable: 1,
     commodity: 1,
     market_tradable_restriction: '7',
      },
  '1644880589_236997301': 
   { appid: '730',
     classid: '1644880589',
     instanceid: '236997301',
     icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ4MAlVo6n3e1Y27OPafjBN09izq42ChfbzNvXTlGkD6p0lj7_FpNjx0VDj_UBoZ272cNfBdg48MAyB-VS3xum61Me_ot2XnqkB5QYc',
     icon_drag_url: '',
     name: 'MLG Columbus 2016 Mirage Souvenir Package',
     market_hash_name: 'MLG Columbus 2016 Mirage Souvenir Package',
     market_name: 'MLG Columbus 2016 Mirage Souvenir Package',
     name_color: 'D2D2D2',
     background_color: '',
     type: 'Base Grade Container',
     tradable: 1,
     marketable: 1,
     commodity: 0,
     market_tradable_restriction: '7',
      },

要处理复杂的对象或集合,可以使用库

因此,json的格式如下:

{
  "success":true,
  "rgInventory": {
    "5719625206": {"id":"5719625206","classid":"1651313004","instanceid":"188530139","amount":"1","pos":1},
    "5719034454": {"id":"5719034454","classid":"1649582636","instanceid":"188530139","amount":"1","pos":2},
    "5718628709": {"id":"5718628709","classid":"1649582636","instanceid":"188530139","amount":"1","pos":3},
    ...
  }
}
要提取所需项的数组,首先在项目中安装lodash库:
npm i-S
,然后将此代码添加到文件中:

var _ = require('lodash');

function extractItems(data) {
  var rgInventory = _.get(data, 'rgInventory');
  if (!rgInventory) {
    return [];
  }
  return _(rgInventory)
    .keys()
    .map(function(id) {
      return _.pick(rgInventory[id], ['classid', 'instanceid']);
    })
    .value();
}

JSON.parse(body)
返回一个JavaScript对象()。要访问任何属性,请看一下这一点,看看它是否有帮助:Thansk需要帮助,但不幸的是,我仍然无法100%理解,我理解您共享的代码,但我不知道如何使用它,如何将它应用于对象,然后使用它。我应该把代码放在哪里以及如何将数据获取到我的对象?嗯,你所需要的只是从你的代码中调用我的函数
extractItems
。将该调用放在解析json的行下方:
var items=extractItems(json)
;我想我的知识太少了,做不到这一点。我会继续做别的事情。无论如何,谢谢你的帮助!嘿我已经读了一点,并决定再次测试。这是我的代码,但是我从你的函数中得到了错误。我做错了什么?代码中有一个Bug,您需要将响应体转换为对象:
data=JSON.parse(data)
{
  "success":true,
  "rgInventory": {
    "5719625206": {"id":"5719625206","classid":"1651313004","instanceid":"188530139","amount":"1","pos":1},
    "5719034454": {"id":"5719034454","classid":"1649582636","instanceid":"188530139","amount":"1","pos":2},
    "5718628709": {"id":"5718628709","classid":"1649582636","instanceid":"188530139","amount":"1","pos":3},
    ...
  }
}
var _ = require('lodash');

function extractItems(data) {
  var rgInventory = _.get(data, 'rgInventory');
  if (!rgInventory) {
    return [];
  }
  return _(rgInventory)
    .keys()
    .map(function(id) {
      return _.pick(rgInventory[id], ['classid', 'instanceid']);
    })
    .value();
}