0

I have the following JSON . I am reading the T3 array .

My question is that is it possible to read the values Can and Bottle

 var jsondata =  {
        "T3": [
            {
                "Can": [
                    {
                        "type": "pepsi can 250ml"
                    },
                    {
                        "type": "pepsi can 100ml"
                    }
                ]
            },
            {
                "Bottle": [
                    {
                        "type": "pepsi bottle 250ml"
                    },
                    {
                        "type": "pepsi bottle 100ml"
                    }
                ]
            }
        ]
    };

I agree that at my knowledge , i can't proceed further from this level ??

for(var c=0;c<jsondata.T3.length;c++)
{


}

But still i want to ask whether this can be done or not ??

3 Answers3

0

Basically you now want to use the c variable to reference the relevant section of the T3 array:

for(var c=0;c<jsondata.T3.length;c++){
  var can_data = jsondata.T3[ c ][ "Can" ]; // T3[ 0 ][ "Can" ]
  var bottle_data = jsondata.T3[ c ][ "Bottle" ]; // T3[ 0 ][ "Bottle" ]
}
Lix
  • 47,311
  • 12
  • 103
  • 131
  • Hi , I used your function this way , please see the jsfiddle http://jsfiddle.net/AyuAY/ , from the obtained result can i extract Can and Bottle from that ?? –  Jun 05 '14 at 09:57
  • I mean to say that , if this line gets executed var result = jsondata.item.T1[a].T2[b].T3[ c ]; , it produces two arrays {"Can":[{"type":"coke can 250ml"},{"type":"coke can 100ml"}]} and {"Bottle":[{"type":"coke bottle 250ml"},{"type":"coke bottle 100ml"}]} , so can i extarct the values Can and bottle from the obtained array . (I need this because Can and Bottle will be fecthed from Database dynamically and i dont konow what values might exist ) –  Jun 05 '14 at 10:01
  • Sure you can - you just reference them as a normal JS object. – Lix Jun 05 '14 at 10:03
  • So should i create an array and push the results ?? but then how can i fetch Can and Bottle from that ?? –  Jun 05 '14 at 10:05
  • you should put the results wherever makes sense in your code. In the same way that you accessed it before: `some_obj[ "Can" ]` – Lix Jun 05 '14 at 10:06
0

It is array inside array so you have to to do like this :

$.each(jsondata,function(index,item){

      $.each(item,function(index1,item1){
         if(item1.Can)
            console.log(item1.Can);
         if(item1.Bottle)
            console.log(item1.Bottle);
      });

});

FIDDLE EXAMPLE

UPDATED:

if you don't know what objects will be in array the you have to do recursive iteration over array:

jQuery recursive iteration over objects

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

pure JavaScript solution

You want to use the Object.keys() method:

And, you'll want to mix it with Array.isArray() and Array#map():

If your environment / browser doesn't have ECMAScript 5 support, then you can find a poly-fill here: https://github.com/es-shims/es5-shim

My approach also uses the following from older JavaScript:

Here's how you might use it:

var result = [];
if (Array.isArray(jsondata.T3)) {
    result = result.concat.apply(result, jsondata.T3.map(function (obj) {
        return Object.keys(obj); // Array of Array of Strings
    }));
    /* Array#concat() used with Function#apply() to reduce
    an Array of Array of Strings down to an Array of Strings */
}
// now, result is Array ['Can', 'Bottle']
jokeyrhyme
  • 3,258
  • 2
  • 20
  • 20