0

I have an index.html file that calls main.php which returns a json object just fine.

main.php returns this

{
    "products": [
        {
            "id": "1",
            "txt": "Bar",
            "image": "",
            "page": ""
        },
        {
            "id": "2",
            "txt": "Car",
            "image": "",
            "page": ""
        },
        {
            "id": "3",
            "txt": "Far",
            "image": "",
            "page": ""
        }
    ],
    "success": 1
}

in Java there is json.getJSONArray(TAG_PRODUCTS);

but.... I am using HTML and javascript here... so in my html file I have the following how do I pull the products array out of my json returned data?

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: 'http://localhost:8888/HelloWorld/main.php',
    contentType: "application/json; charset=utf-8",
    success: function (data) { 
        var names = data
        $('sa_content').append('<select name="input_4">');
        $.each(data, function(index, element) {

            alert( "index: " + index + ", element.cf_id: " + element );

        });
        $('sa_content').append('</select>');
    },
    error: function(){
        $('#sa_content').append('<li>There was an error loading the feed');
    }
});

When I run the index.html page I get the following alerts

index = products, element =  [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

and

index = success, element =  1

So how do I pull the products array out of the json and iterate through it to get the values of id, txt, image, page?

Thanks in advance for your time. Dean-O

4 Answers 4

3

Try using this as the success function:

function (data) {
    var products = data.products;

    for (var i = 0; i < products.length; i++) {
        var product = products[i];
        alert(product.id + " " + product.txt + " " + product.image + " " + product.page);
    }
}

It should iterate through all the products and alert you the data. Then you can modify the code to suit your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent... thanks for clearing it up. Worked like a champ.
0

You either use JSON.parse, or simply eval('(' + json + ')'); if you are certain the code is safe.

Comments

0

You're getting an array of products, so you want to iterate over that array:

success: function (data) { 
    var names = data

    for(i = 0; i < data.products.length; i++) {
        //Do what you want.
        var id = data.products[i].id;
    }
}

Comments

0

If your page returns ONLY json, then the XHR object that comes back from your jquery ajax should contain a javascript object constructed from that data. If that is not the case, something is wrong with your page, such as returning more than a json encoded string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.