3

下の画像に示す JSON があります。0アンダーアイテム というオブジェクトがあります。

アイテム内の属性を取得したい。jQuery または Javascript でアクセスするにはどうすればよいですか?

ここに画像の説明を入力

これは私がこれまでに持っているものです:

 jQuery.getJSON('http://localhost/magento/js/primitives/js/SpaceTreeRegion.json',
     function(jsonData) {
     jQuery.each(jsonData, function(currentRegionNode, jsonData) {
         alert(jsonData.items.product_name);
     });
}); 

このコードはエラーを返します:jsonData.items.value is undefined

JSON は次のようになります。

  [
      {
          "category_id": "Clothes",
          "Items": [
              {
                  "product_id": 1,
                  "price": 50,
                  "product_name": "Shirts"
              },
              {
                  "product_id": 2,
                  "price": 60,
                 "product_name": "T-Shirt"
              }
          ]
     }
 ]

オブジェクト内のproduct_idおよびpricefor product_nameeachへのアクセスが必要です。items

4

5 に答える 5

4

このコードを試してください

  jQuery.getJSON('your json url', function(yourJsonData) {
     jQuery.each(yourJsonData[0].Items, function(key, customData) {
            alert(customData.product_name);
    });
 });     
于 2014-01-03T11:12:21.830 に答える
2

このコードはあなたにアイデアを与えるはずです:

例: alert(jsonData[0].Items[0].product_id);

この $.each を試してください:

`$.each(jsonData[0].Items, function (i, item) {
            alert(item.product_id);
            alert(item.price);
            alert(item.product_name);
        });`
于 2014-01-03T07:37:43.223 に答える
1

このjsonファイルを試してください:

 {
      "category_id": "Clothes",
      "Items": [
          {
              "product_id": 1,
              "price": 50,
              "product_name": "Shirts"
          },
          {
              "product_id": 2,
              "price": 60,
             "product_name": "T-Shirt"
          }
      ]
 }

次に、次のように解析します。

$.each(jsonData.Items, function(index, value){
  alert(value.product_name)
});
于 2014-01-03T07:30:20.813 に答える
0

それはアイテムに価値がないからです。

for(var i=0; i<jsonData.items.length; i++){
    var youritem = jsonData.items[i]; /*this is your item*/
    console.log(jsonData.items[i].product_name);/*one of your product names*/
}

ここにあなたのコードがあります:

 jQuery.getJSON('http://localhost/magento/js/primitives/js/SpaceTreeRegion.json',
       function(jsonData) {
 jQuery.each(jsonData, function(currentRegionNode, jsonData) {

                   alert(this.items);/*this is an object also*/
                   alert(this.items[0].product_name)/*this is your child arrays property*/
                   alert(this.items[1].product_name)/*this is second one*/

});

});

this.itemsもう一度ループできるように

于 2014-01-03T07:36:34.837 に答える