【问题标题】:Printing parsed JSON打印解析的 JSON
【发布时间】:2015-12-05 16:37:16
【问题描述】:

所以,我解析了这样的响应:

var g_rgListingInfo = JSON.parse( response );

响应看起来像这样

{"321242653847396921":{"listingid":"321242653847396921","price":28338,"fee":4249,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003" ,"steam_fee":1416,"publisher_fee":2833,"asset":{"currency":0,"appid":730,"contextid":"2","id":"3038615825","amount": "1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"在游戏中检查... "}]}},"321242653843485871":{"listingid":"321242653843485871","price":30175,"fee":4525,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":" 2003","steam_fee":1508,"publisher_fee":3017,"asset":{"currency":0,"appid":730,"contextid":"2","id":"1730491611","amount ":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"在游戏中检查。 .."}]}},"782860982384213986":{"listingid":"782860982384213986","price":31305,"fee":4695, "publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1565,"publisher_fee":3130,"asset":{"currency":0,"appid":730 ,"contextid":"2","id":"2815962367","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M %listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"783987515556891867":{"listingid":"783987515556891867","price":31305,"fee": 4695,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1565,"publisher_fee":3130,"asset":{"currency":0,"appid" :730,"contextid":"2","id":"3708699202","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview %20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"783987515558623437":{"listingid":"783987515558623437","price":30957,"fee ":4642,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":" 2003","steam_fee":1547,"publisher_fee":3095,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4462433815","金额":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"在游戏中检查。 .."}]}},"718685320959305952":{"listingid":"718685320959305952","price":34000,"fee":5100,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid" :"2001","steam_fee":1700,"publisher_fee":3400,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4450043953", "amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"检查游戏..."}]}},"796369492002647568":{"listingid":"796369492002647568","price":34500,"fee":5175,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612"," currencyid":"2001","steam_fee":1725,"publisher_fee":3450,"asset":{"currency":0,"appid":730,"conte xtid":"2","id":"4024113558","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid %A%assetid%D3082226233578562378","name":"Inspect in Game..."}]}},"718684619833530742":{"listingid":"718684619833530742","price":22958,"fee":3442, "publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2002","steam_fee":1147,"publisher_fee":2295,"asset":{"currency":0,"appid":730 ,"contextid":"2","id":"4331886445","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M %listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"788487401257494747":{"listingid":"788487401257494747","price":34783,"fee": 5217,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2001","steam_fee":1739,"publisher_fee":3478,"asset":{"currency":0,"appid" :730,"contextid":"2","id":"2315637005","amount":"1","market_actions":[{"link":"steam://rungame/ 730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"321242020664839911":{"listingid":"3212420206664" ":34783,"fee":5217,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2001","steam_fee":1739,"publisher_fee":3478,"asset":{"货币":0,"appid":730,"contextid":"2","id":"4283078084","amount":"1","market_actions":[{"link":"steam:// rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D6944696178921031564","name":"Inspect in Game..."}]}}}

我把它放在这里:http://json.parser.online.fr/ 这是 result

我遇到的问题是我无法遍历这些项目。 g_rgListingInfo.length 为 NaN。我尝试使用 forEach 但也失败了。

我想遍历所有这些“321242653847396921”、“321242653843485871”...它们总是在变化并获取它们的listingid、价格、费用等。

我对 node.js 很陌生,所以如果这是一个愚蠢的问题,我很抱歉。

【问题讨论】:

    标签: json node.js loops


    【解决方案1】:

    你有一个对象,而不是一个数组。因此,要迭代结果,您必须将对象转换为数组或将其作为对象进行迭代。

    转换成数组

    根据您的需要,这可能更方便:

    var myData = Object.keys(g_rgListingInfo).map(Number).sort().map(function (c) {
         return g_rgListingInfo[c];
    });
    
    // Then you can just use the `myData` array
    myData.forEach(function (current, index) {
        /* do something */
    });
    
    // ...or using for loop
    for (var i = 0; i < myData.length; ++i) {
        var current = myData[i];
        /* do something */
    }
    

    迭代对象

    您必须获取对象的键来迭代它们(或者您可能希望先对它们进行排序)。

    // ["3124...", ...]
    var numbers = Object.keys(g_rgListingInfo);
    
    // Optional sort
    numbers = numbers.map(Number).sort();
    
    // Iterate the object keys
    numbers.forEach(function (currentKey) {
        var currentObject = g_rgListingInfo[currentKey];
    });
    

    【讨论】:

      猜你喜欢
      • 2011-06-23
      • 2018-09-11
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多