3

I have some JSON shown in the image below. There is an object called 0 under items.

I want to get the attributes inside items. How can I access it with jQuery or Javascript?

enter image description here

This is what I have so far:

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

This code gives an error: jsonData.items.value is undefined

The JSON is like this:

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

I want access to product_id,price and product_name for each items in an Object.

5 Answers 5

4

try this code

  jQuery.getJSON('your json url', function(yourJsonData) {
     jQuery.each(yourJsonData[0].Items, function(key, customData) {
            alert(customData.product_name);
    });
 });     
Sign up to request clarification or add additional context in comments.

Comments

2

This code should give you the idea:

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

Try this $.each then:

`$.each(jsonData[0].Items, function (i, item) {
            alert(item.product_id);
            alert(item.price);
            alert(item.product_name);
        });`

1 Comment

this is giving only one result...and i don't want use for loop.
1

Try this json file:

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

Then parse it like this:

$.each(jsonData.Items, function(index, value){
  alert(value.product_name)
});

3 Comments

what you trying to say?
There was additional [] outside of the data, what results in an extra array in the parsed data.
Yes, but you are doing the same mistake later in the code. There is no jsonData.items.value. You also don't need to go use jQuery.each(jsonData, function(currentRegionNode, jsonData). You have jsonData.Items[0].product_name and others. jsonData represents all the data from the json, then there is an Items array which holds objects.
0

Thats because Items hasnt value.

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*/
}

here is your code:

 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*/

});

});

so you can loop in this.items again

7 Comments

this is taking to much time....so i want to do it with out for loop....batter i can you jQuery
ok your main idea is loop in json with each. but you have two arrays. and you loop parent one. do same thing for child array.
yes right..but when i access to child node it's giving error "jsonData.items is undefine"
your oloping in categories you should also loop items for each categories.
this.Items[0].product_name
|
0

Change your each loop to something like this -

jQuery.each(jsonData['Items'], function(currentRegionNode, jsonItemData) {
    alert(jsonItemData.product_name); // will output 'Shirts ', 'T-Shirt'
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.