-1

I have the following JSON which I am parsing using a generic function and expecting a return value.

{
    "items":
    {
        "item":
        {
            "Beverages":
                [

                    {
                        "id": "0004",
                        "name": "Thums Up",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Can":[ "250ml", "300ml","330ml", {"image":"images/0001.jpg" }],
                        "Bottle":[ "350ml",  "600ml", {"image":"images/0001.jpg" } ],
                        "Fountain":["small", "large", {"image":"images/0001.jpg" }]
                    },
                    {
                        "id": "0005",
                        "name": "Pepsi",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Can":[ "250ml", "300ml","330ml", {"image":"images/0001.jpg" }],
                        "Bottle":[ "350ml",  "600ml", {"image":"images/0001.jpg" } ],
                        "Fountain":["small", "large", {"image":"images/0001.jpg" }]
                    }
                ],

            "Snacks":
                [
                    {
                        "id": "0010",
                        "name": "Puffs",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Veg":["Veg puff", {"image":"images/0001.jpg" }],
                        "Egg":["Egg puff", {"image":"images/0001.jpg" }],
                        "Chicken":["Chicken puff", {"image":"images/0001.jpg" }]
                    },
                    {
                        "id": "0011",
                        "name": "Samosa",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Aloo Samosa":["small", "big", {"image":"images/0001.jpg" }],
                        "Corn Samosa":["small", "big", {"image":"images/0001.jpg" }]
                    }
                ]

        }
    }
}

In my program, I am passing the value and expecting an array from that.

This is my complete program. I am using jQuery for this.

http://jsfiddle.net/DH8K2/

I am doing it this way:

$(document).ready(function (jsondata) {
    var value = 'Beverages';
    var retdata[] = returnValues(value);
    for (int i = 0; i < retdata.length; i++) {
        alert(retdata[i]);
    }
});

function returnValues(value) {
    var data = [];
    $.each(jsondata.items.item, function (i, v) {
        if (i == value) {
            data.push[i];
        }
    });

    return data;
}

I am a Java developer with very minimal JS knowledge so if this way is completely wrong also please let me know, so that I can change that accordingly.

sorry if this is incomlete question ,and made you confusing .

my question is I mean if you see the json above ,and if i pass the value Pepsi value directly , can i get image , Can , Bottle and Fountain values ?? and similarly if i pass the value Beverages , i should get all the child elemnts of Beverages

  • 3
    What's your question? – JJJ May 28 '14 at 11:29
  • I mean if you see the json above ,and if i pass the value Pepsi value directly , can i get image , Can , Bottle and Fountain values ?? and similarly if i pass the value Beverages , i should get all the child elemnts of Beverages –  May 28 '14 at 11:32
  • 1
    You have several syntactical errors, you should really use a debugger: `var retdata[] = returnValues(value);` should be `var retdata = returnValues(value);`, `for (int i = 0; i < retdata.length; i++)` should be `for (var i = 0; i < retdata.length; i++)`... – Korikulum May 28 '14 at 11:33
  • @Korikulum , initially i tried with taht approach only , but it threw an error in console saying undefined . –  May 28 '14 at 11:35
  • What exactly was undefined, which variable? – Korikulum May 28 '14 at 11:36

2 Answers2

0

It looks like you wrote this in Java, then tried to change a few keywords to get it to work in your browser.

Problems you had:

  1. No such keyword as int

  2. .push() needs (), not [] as it's a function call

  3. Variable types are not explicit in Javascript. No need for [] after a variable name to define an array.

Here's a JSFiddle that works.

Sam P
  • 1,821
  • 13
  • 26
  • thanks for the answer , incase i pass Beverages , is it possible that it returns me the child element names of it , that is pepsi and thumps up in this case ?? –  May 28 '14 at 11:40
  • If you do another loop to go through each Beverages object properties, then yes. – Sam P May 28 '14 at 11:45
  • Thanks Sam , instead of this way , is it possible to write a generic function so that on passing the name it will return me the array of that ?? –  May 28 '14 at 11:53
  • A generic function to deep search an object for arrays matching a given name? – Sam P May 28 '14 at 11:54
  • http://stackoverflow.com/questions/15642494/find-property-by-name-in-a-deep-object – Sam P May 28 '14 at 11:57
  • Thanks that will definately help me , meanwhile if i pass Beverages , to get the names (pepsi and thumpsup ), i tried to retrive it using if (i == value) { //data.push(i); alert(v.name); } , but it gave undefined . what wrong i am doing here ?? –  May 28 '14 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54626/discussion-between-sam-p-and-kiran). – Sam P May 28 '14 at 12:03
0

maybe this is what you're looking for:

var obj = $.parseJSON(dataJson);
Andi Sholihin
  • 479
  • 3
  • 8