简体   繁体   English

试图在Json对象中循环,对象中包含更多对象

[英]Trying to loop through Json Object that has more objects inside with arrays of objects

I've been beating my head against the wall trying to loop through this json file. 我一直在试图绕过这个json文件。

when I console log this it says that the Response is an Object, data is an object, saleItemCategories is an array, which has four objects the first being vehicles which is an array of item objects. 当我控制日志时,它表示响应是一个对象,数据是一个对象,saleItemCategories是一个数组,它有四个对象,第一个是车辆,它是一个项目对象数组。

Basically I am trying to get the various values from these item objects. 基本上我试图从这些项目对象中获取各种值。 At this point I'm just looking for help drilling down to this level. 在这一点上,我只是寻求帮助深入到这个水平。

"Response":{
  "data":{
     "vendorHash":2668878854,
     "nextRefreshDate":"9999-12-31T23:59:59.999Z",
     "enabled":true,
     "saleItemCategories":[
        {
           "categoryIndex":2,
           "categoryTitle":"Vehicles",
           "saleItems":[
              {
                 "item":{
                    "itemHash":3645385954,
                    "bindStatus":0,
                    "isEquipped":false,
                    "itemInstanceId":"0",
                    "itemLevel":0,
                    "stackSize":1,
                    "qualityLevel":0,
                    "stats":[
                       {
                          "statHash":3017642079,
                          "value":35,
                          "maximumValue":100
                       }
                    ],
                    "primaryStat":{
                       "statHash":1501155019,
                       "value":150,
                       "maximumValue":0
                    },
                    "canEquip":true,
                    "equipRequiredLevel":0,
                    "unlockFlagHashRequiredToEquip":2166136261,
                    "cannotEquipReason":0,
                    "damageType":0,
                    "damageTypeHash":0,
                    "damageTypeNodeIndex":-1,
                    "damageTypeStepIndex":-1,
                    "progression":{
                       "dailyProgress":0,
                       "weeklyProgress":0,
                       "currentProgress":0,
                       "level":0,
                       "step":0,
                       "progressToNextLevel":0,
                       "nextLevelAt":0,
                       "progressionHash":0
                    },
                    "talentGridHash":2064730449,
                    "nodes":[
                       {
                          "isActivated":true,
                          "stepIndex":0,
                          "state":10,
                          "hidden":false,
                          "nodeHash":0
                       }
                    ],
                    "useCustomDyes":true,
                    "artRegions":{

                    },
                    "isEquipment":true,
                    "isGridComplete":false,
                    "perks":[
                       {
                          "iconPath":"/common/destiny_content/icons/b65252b2007607955aa1fc3026154c8c.png",
                          "perkHash":4091143788,
                          "isActive":true
                       }
                    ],
                    "location":3,
                    "transferStatus":0,
                    "locked":false,
                    "lockable":false,
                    "objectives":[

                    ],
                    "state":0
                 },
                 "vendorItemIndex":30,
                 "itemStatus":0,
                 "costs":[
                    {
                       "itemHash":2534352370,
                       "value":175
                    }
                 ],
                 "requiredUnlockFlags":[

                 ],
                 "unlockStatuses":[

                 ],
                 "failureIndexes":[

                 ]
              },

I have tried looping through each Response as object, then for each data as object, then I tried looping through the saleItemCategories array, but this is usually where it breaks 我已经尝试循环遍历每个Response作为对象,然后为每个数据作为对象,然后我尝试循环遍历saleItemCategories数组,但这通常是它打破的地方

var json = "your data";

var itemCategories = json.Response.data.saleItemCategories;

for (int i = 0; i < itemCategories.length; i++) {
  var categoryItems = itemCategories[i].saleItems;
  console.log(categoryItems.categoryTitle);

  for (int j = 0; j < categoryItems.length; j++) {
    var categoryItem = categoryItems[j];
    var categoryItemStats = categoryItem.stats;

    console.log(categoryItem.itemHash);

    for (var k = 0; k < categoryItemStats; k++) {
      var categoryItemStat = categoryItemStats[k];

      console.log(categoryItemStat);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM