Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Data from a Json Object with JavaScript

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.

like image 300
thumber nirmal Avatar asked Dec 04 '25 22:12

thumber nirmal


1 Answers

try this code

  jQuery.getJSON('your json url', function(yourJsonData) {
     jQuery.each(yourJsonData[0].Items, function(key, customData) {
            alert(customData.product_name);
    });
 });     
like image 177
Jaydeep Pandya Avatar answered Dec 06 '25 14:12

Jaydeep Pandya