某些JSON对象未定义

时间:2017-05-13 09:57:10

标签: json node.js

所以我正在使用nodeJS和Steam API来尝试创建一个交易机器人 我需要知道收到了什么物品,但是当我这样做时我会得到

undefined

这是JSON:

    {  
   "partner":{  
      "universe":1,
      "type":1,
      "instance":1,
      "accountid":369917733
   },
   "id":"2112720622",
   "message":"No need to accept, just testing out a bot.",
   "state":3,
   "itemsToGive":[  

   ],
   "itemsToReceive":[  
      {  
         "appid":730,
         "contextid":"2",
         "assetid":"10168993563",
         "classid":"1989300153",
         "instanceid":"302028390",
         "amount":1,
         "missing":true,
         "icon_url":"IzMF03bi9WpSBq-S-ekoE33L-iLqGFHVaU25ZzQNQcXdB2ozio1RrlIWFK3UfvMYB8UsvjiMXojflsZalyxSh31CIyHz2GZ-KuFpPsrTzBG0qe6yD3n-ZDLdYXKOTVxqTudcPGjZrGLx5ujCF2nASeh-EF9XdfEAo2JLNZiKahtv0oMVu2u_0UdyEhk6f9BKZAarxm1OMLh9m3IWGBD1M58",
         "icon_url_large":"IzMF03bi9WpSBq-S-ekoE33L-iLqGFHVaU25ZzQNQcXdB2ozio1RrlIWFK3UfvMYB8UsvjiMXojflsZalyxSh31CIyHz2GZ-KuFpPsrTzBG0qe6yD3n-ZDLdEC3YDlltU-cLND7c92Hxt-SVFGqfFe54SgACdfcFoDJPP82AOBtrhdZZ-Ga9zhwzDhgvNMZJfACpx2EfJbQ1xDhPJM5amiekAf1NktA",
         "icon_drag_url":"",
         "name":"Sealed Graffiti | Still Happy (Desert Amber)",
         "market_hash_name":"Sealed Graffiti | Still Happy (Desert Amber)",
         "market_name":"Sealed Graffiti | Still Happy (Desert Amber)",
         "name_color":"D2D2D2",
         "background_color":"",
         "type":"Base Grade Graffiti",
         "tradable":true,
         "marketable":true,
         "commodity":true,
         "market_tradable_restriction":7,
         "fraudwarnings":[  

         ],
         "descriptions":[  
            {  
               "type":"html",
               "value":"This is a sealed container of a graffiti pattern. Once this graffiti pattern is unsealed, it will provide you with enough charges to apply the graffiti pattern <b>50</b> times to the in-game world.",
               "app_data":""
            },
            {  
               "type":"html",
               "value":" ",
               "app_data":""
            },
            {  
               "type":"html",
               "value":"",
               "color":"00a000",
               "app_data":{  
                  "limited":"1"
               }
            }
         ],
         "owner_descriptions":[  

         ],
         "actions":[  
            {  
               "name":"Inspect in Game...",
               "link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D9253357711394385702"
            }
         ],
         "market_actions":{  
            "0":{  
               "name":"Inspect in Game...",
               "link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D9253357711394385702"
            }
         },
         "tags":[  
            {  
               "internal_name":"CSGO_Type_Spray",
               "name":"Graffiti",
               "category":"Type",
               "category_name":"Type"
            },
            {  
               "internal_name":"normal",
               "name":"Normal",
               "category":"Quality",
               "category_name":"Category"
            },
            {  
               "internal_name":"Rarity_Common",
               "name":"Base Grade",
               "category":"Rarity",
               "color":"b0c3d9",
               "category_name":"Quality"
            },
            {  
               "internal_name":"Tint5",
               "name":"Desert Amber",
               "category":"SprayColorCategory",
               "category_name":"Graffiti Color"
            }
         ],
         "id":"10168993563",
         "owner_actions":[  

         ],
         "market_marketable_restriction":0
      }
   ],
   "isOurOffer":true,
   "created":"2017-05-13T09:50:24.000Z",
   "updated":"2017-05-13T09:50:48.000Z",
   "expires":"2017-05-27T09:50:24.000Z",
   "tradeID":"1915564581201455085",
   "fromRealTimeTrade":false,
   "confirmationMethod":0,
   "escrowEnds":null,
   "rawJson":"{\n\t\"tradeofferid\": \"2112720622\",\n\t\"accountid_other\": 369917733,\n\t\"message\": \"No need to accept, just testing out a bot.\",\n\t\"expiration_time\": 1495878624,\n\t\"trade_offer_state\": 3,\n\t\"items_to_receive\": [\n\t\t{\n\t\t\t\"appid\": \"730\",\n\t\t\t\"contextid\": \"2\",\n\t\t\t\"assetid\": \"10168993563\",\n\t\t\t\"classid\": \"1989300153\",\n\t\t\t\"instanceid\": \"302028390\",\n\t\t\t\"amount\": \"1\",\n\t\t\t\"missing\": true\n\t\t}\n\t],\n\t\"is_our_offer\": true,\n\t\"time_created\": 1494669024,\n\t\"time_updated\": 1494669048,\n\t\"tradeid\": \"1915564581201455085\",\n\t\"from_real_time_trade\": false,\n\t\"escrow_end_date\": 0,\n\t\"confirmation_method\": 0\n}"
}

我可以很容易地获得合作伙伴,id,消息,状态,isOurOffer和所有时间的东西,但是当我尝试获取itemsToRecieve时,它只是抛出未定义的

通过一个事件收集JSON,上面是我收到的确切JSON

这是事件:

manager.on('sentOfferChanged', function(offer, oldState) {
    var state = offer.state;
    if(state == 3) {
        console.log("Offer " + offer.id + " was accepted");
        console.log("[Offer #" + offer.id + "] The following items was recieved: ");
        console.log(offer.itemsToReceive);
    }
});

为什么只对其中一个对象执行此操作?

1 个答案:

答案 0 :(得分:0)

也许您可以使用for来记录itemToReceive的每个项目。

这样的事情:

manager.on('sentOfferChanged', function(offer, oldState) {
    var state = offer.state;
    if(state == 3) {
        console.log("Offer " + offer.id + " was accepted");
        console.log("[Offer #" + offer.id + "] The following items was recieved: ");

       for(var i in itemstoReceive)
         {
           var name = itemsToReceive[i].name;
           console.log(name);
             }}
});