计算AJAX中的JSON节点不起作用

时间:2017-02-15 03:56:04

标签: php jquery json ajax

好的,我有以下JSON代码:

{
"productDetails": {
    "productThumb": "/uploads/testAdmin@dixoncom.com/showup-bg4.jpg",
    "productName": "Hamburger",
    "productDesc": "Made with 100% American ground beef",
    "productPrice": "9.99",
    "productModifiers": [{
        "Sides": [{
            "modifierName": "Applesauce",
            "modifierUpcharge": "0",
            "isDefault": "off"
        },{
            "modifierName": "Rice",
            "modifierUpcharge": "0",
            "isDefault": "off"
        }],
        "Toppings": [{
            "modifierName": "Bacon",
            "modifierUpcharge": "0.25",
            "isDefault": "off"
        }, {
            "modifierName": "Tomato",
            "modifierUpcharge": "0",
            "isDefault": "on"
        }],
        "Doneness": [{
            "modifierName": "Extra Well",
            "modifierUpcharge": "0",
            "isDefault": "off"
        },{
            "modifierName": "Well",
            "modifierUpcharge": "0",
            "isDefault": "off"
        }],
        "Breads": [{
            "modifierName": "White Bun",
            "modifierUpcharge": "0",
            "isDefault": "on"
        }, {
            "modifierName": "Whole Wheat Bun",
            "modifierUpcharge": "0",
            "isDefault": "off"
        }]
    }]
}
}

我通过jsonlint.com运行它并验证它是有效的JSON。在我的jQuery中,我有以下代码:

$.ajax({
    url: "/getItemDetails/"+productType+"/"+productID,
    type: "get",
    data: ({productType:productType, productID:productID}),
    dataType: "json",
    success: function(data) {
        var numMods = data.productDetails.productModifiers.length;
        console.log("There are "+numMods+" categories of modifiers.");
    }
});

出于某种原因,我的console.log返回为undefined。基本上,我需要知道有多少"类别"在productModifiers节点内。所以在这种情况下,它应该返回4(Sides,Toppings,Doneness和Breads)。我似乎无法弄清楚我错过了哪些东西可以让我弄清楚有多少个子节点

2 个答案:

答案 0 :(得分:0)

我猜即使你的productModifiers拥有一个阵列;该数组只包含4个键的单个对象。您能否尝试使用以下代码:

var data = {
  "productDetails": {
    "productThumb": "/uploads/testAdmin@dixoncom.com/showup-bg4.jpg",
    "productName": "Hamburger",
    "productDesc": "Made with 100% American ground beef",
    "productPrice": "9.99",
    "productModifiers": [{
      "Sides": [{
        "modifierName": "Applesauce",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }, {
        "modifierName": "Rice",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }],
      "Toppings": [{
        "modifierName": "Bacon",
        "modifierUpcharge": "0.25",
        "isDefault": "off"
      }, {
        "modifierName": "Tomato",
        "modifierUpcharge": "0",
        "isDefault": "on"
      }],
      "Doneness": [{
        "modifierName": "Extra Well",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }, {
        "modifierName": "Well",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }],
      "Breads": [{
        "modifierName": "White Bun",
        "modifierUpcharge": "0",
        "isDefault": "on"
      }, {
        "modifierName": "Whole Wheat Bun",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }]
    }]
  }
};

var productModifiersObj = data.productDetails.productModifiers[0];

var count = 0;
var i;

for (i in productModifiersObj) {

  if (productModifiersObj.hasOwnProperty(i)) {
    count++;
  }
}

alert("There are " + count + " categories of modifiers.");

答案 1 :(得分:0)

问题是,productModifiers是一个数组,其中包含一个对象,其中包含您需要的类别的多个键。因此,下面是您需要的代码。如果productModifiers可以有超过1个对象,那么您需要遍历对象以计算您的类别。



var json = {
  "productDetails": {
    "productThumb": "/uploads/testAdmin@dixoncom.com/showup-bg4.jpg",
    "productName": "Hamburger",
    "productDesc": "Made with 100% American ground beef",
    "productPrice": "9.99",
    "productModifiers": [{
      "Sides": [{
        "modifierName": "Applesauce",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }, {
        "modifierName": "Rice",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }],
      "Toppings": [{
        "modifierName": "Bacon",
        "modifierUpcharge": "0.25",
        "isDefault": "off"
      }, {
        "modifierName": "Tomato",
        "modifierUpcharge": "0",
        "isDefault": "on"
      }],
      "Doneness": [{
        "modifierName": "Extra Well",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }, {
        "modifierName": "Well",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }],
      "Breads": [{
        "modifierName": "White Bun",
        "modifierUpcharge": "0",
        "isDefault": "on"
      }, {
        "modifierName": "Whole Wheat Bun",
        "modifierUpcharge": "0",
        "isDefault": "off"
      }]
    }]
  }
}


var numMods = Object.keys(json.productDetails.productModifiers[0]).length;
console.log("There are " + numMods + " categories of modifiers.");