打印已解析的JSON

时间:2015-12-05 16:37:16

标签: json node.js loops

所以,我已经解析了这样的回复:

var g_rgListingInfo = JSON.parse( response );

响应看起来像这样

  

{" 321242653847396921" {" listingid":" 321242653847396921""价格":28338"费用&#34 ;:4249," publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2003&#34 ;," steam_fee":1416," publisher_fee":2833,"资产" {"货币":0," APPID& #34; 730"关联标识符":" 2"" ID":" 3038615825""量" :" 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid %A%assetid%D1030942533801731526","名称":"在游戏中检查..."}]}}," 321242653843485871":{&# 34; listingid":" 321242653843485871""价格":30175"费用":4525," publisher_fee_app":730,& #34; publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2003"" steam_fee":1508,&#34 ; publisher_fee":3017,"资产" {" CURR ency":0," APPID" 730"关联标识符":" 2"" ID":" 1730491611& #34;"量":" 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame / 730/76561202255233023 / + csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526"," name":" Inspect in Game ..."}]}}, #&34; 782860982384213986" {" listingid":" 782860982384213986""价格":31305"费用":4695, " publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2003"&# 34; steam_fee":1565," publisher_fee":3130,"资产" {"货币":0," APPID&#34 ;: 730,"关联标识符":" 2"" ID":" 2815962367""量":&#34 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid%A%由assetid %D1030942533801731526","名称":"在游戏中检查..."}]} }," 783987515556891867" {" listingid":" 783987515556891867""价格":31305"费用&#34 ;: 4695" publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2003&#34 ;, " steam_fee":1565," publisher_fee":3130,"资产" {"货币":0," APPID&#34 ; 730"关联标识符":" 2"" ID":" 3708699202""量":& #34; 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid%A %assetid%D1030942533801731526"," name":" Inspect in Game ..."}]}}," 783987515558623437":{" listingid":" 783987515558623437""价格":30957"费用":4642," publisher_fee_app" 730&#34 ; publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2003"" steam_fee":1547," publisher_fee& #34;:3095,"资产" {" CURREN CY和#34;:0," APPID" 730"关联标识符":" 2"" ID":" 4462433815& #34;"量":" 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame / 730/76561202255233023 / + csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526"," name":" Inspect in Game ..."}]}}, #&34; 718685320959305952" {" listingid":" 718685320959305952""价格":34000"费用":5100, " publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2001"&# 34; steam_fee" 1700," publisher_fee":3400,"资产" {"货币":0," APPID&#34 ;: 730,"关联标识符":" 2"" ID":" 4450043953""量":&#34 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid%A%由assetid %D1030942533801731526","名称":"在游戏中检查..."}]}}, #&34; 796369492002647568" {" listingid":" 796369492002647568""价格":34500"费用":5175, " publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2001"&# 34; steam_fee":1725," publisher_fee":3450,"资产" {"货币":0," APPID&#34 ;: 730,"关联标识符":" 2"" ID":" 4024113558""量":&#34 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid%A%由assetid %D3082226233578562378","名称":"在游戏中进行检查..."}]}}," 718684619833530742":{" listingid&# 34;:" 718684619833530742""价格":22958"费用":3442," publisher_fee_app" 730" publisher_fee_percent& #34;:" 0.10000000149011612"" currencyid":" 2002"" steam_fee":1147," publisher_fee&#34 ;:2295,"资产" {"货币":0," APPID" 730"关联标识符":" 2"" ID":" 4331886445& #34;"量":" 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame / 730/76561202255233023 / + csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526"," name":" Inspect in Game ..."}]}}, #&34; 788487401257494747" {" listingid":" 788487401257494747""价格":34783"费用":5217, " publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2001"&# 34; steam_fee":1739," publisher_fee":3478,"资产" {"货币":0," APPID&#34 ;: 730,"关联标识符":" 2"" ID":" 2315637005""量":&#34 1"" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid%A%由assetid %D1030942533801731526","名称":"在游戏中进行检查..."}]}},&# 34; 321242020664839911" {" listingid":" 321242020664839911""价格":34783"费用":5217,&# 34; publisher_fee_app" 730" publisher_fee_percent":" 0.10000000149011612"" currencyid":" 2001"" steam_fee":1739," publisher_fee":3478,"资产" {"货币":0," APPID":730, "关联标识符":" 2"" ID":" 4283078084""量":" 1& #34;" market_actions&#34 ;: [{"连结":"蒸汽:// rungame /76561202255233023分之730/ + csgo_econ_action_preview%20M%listingid%A%由assetid%D6944696178921031564& #34;,"名称":"在游戏中进行检查..."}]}}}

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

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

我想循环遍历所有这些" 321242653847396921"," 321242653843485871" ...总是在变化并获取他们的列表,价格,费用等。

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

1 个答案:

答案 0 :(得分:0)

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

转换为数组

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

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];
});