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