1

I am getting this output of json by doing this

$.get('getDataPartySize.php', function(data) {
      alert(data);
});

Output of this is :

[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object]

and I tried ouput of getDataPartySize.php like below and getDataPartySize.php have also this header("Content-type: text/json");

[{"name":"waqar","size":"3"},{"name":"waqas","size":"8"},{"name":"jahangir","size":"6"},{"name":"wawar","size":"3"},
 {"name":"basit","size":"5"},{"name":"basit","size":"5"},{"name":"Ali","size":"4"},
 {"name":"basit","size":"4"},{"name":"Malik","size":"5"},{"name":"basit","size":"5"},
 {"name":"Kashif","size":"7"},{"name":"test","size":"5"},{"name":"Ch","size":"7"},
 {"name":"first","size":"10"},{"name":"2nd","size":"6"},{"name":"qasim","size":"10"},
 {"name":"saleem","size":"14"},{"name":"testing","size":"16"},
 {"name":"kashif","size":"10"},{"name":"Ch","size":"14"},{"name":"saleem","size":"6"},
 {"name":"new","size":"1"},{"name":"abc","size":"2"},{"name":"lahori","size":"3"},
 {"name":"newpak","size":"1"},{"name":"abc","size":"2"},{"name":"tabaq","size":"6"}
]

I am using jQuery. I don't know what the problem is.

EDIT: also tried this, getting same output.

$.getJSON("getDataPartySize.php",function(result){
  alert(result);
}
2
  • It may not matter, but what version of jquery are you using?
    – Jared
    Jan 26, 2012 at 20:16
  • 1
    Try to console.log instead of alert. alert tries to convert the objects to strings (which is [object Object]).
    – gen_Eric
    Jan 26, 2012 at 20:16

5 Answers 5

2

specify the dataType

$.get('getDataPartySize.php', function(data) {
      alert(data);
},'json');

or parse the json manually

$.get('getDataPartySize.php', function(data) {
var json = $.parseJSON(data);
      alert(json);
//i would prefer 
//console.log(json);
//because alert tries to convert the objects to strings as mentioned by @Rocket in comments
});

here is the DEMO

5
  • The dataType is optional, jQuery will detect what it is and parse accordingly. He has Content-type: text/json, so jQuery will automagically realize it's JSON, but adding the dataType is always a good idea.
    – gen_Eric
    Jan 26, 2012 at 20:22
  • @Rocket i missed the header("Content-type: text/json"); of the question thanks for correcting
    – Rafay
    Jan 26, 2012 at 20:24
  • Adding 'json' is a good idea anyway, it never hurt to be too verbose.
    – gen_Eric
    Jan 26, 2012 at 20:25
  • One more thing, i want to get the value of each field as traverse one object like {"name":"abc","size":"4"} and get the value of name and size on one iteration of $.each ? is it possible ? Jan 26, 2012 at 20:47
  • @ClusterStorms: $.each(data, function(){console.log(this.name+'='+this.size);});
    – gen_Eric
    Jan 26, 2012 at 21:08
2

Your JSON is correct, you're seeing [object Object] because alert tries to convert the objects to strings. alert isn't the best for debugging. You should use console.log and then look in your JS console.

If you use console.log, you'll see it's just fine. Try to alert(data[0].name), and you'll see it works fine.

P.S. You should set the type to 'json' in the $.get. jQuery will automagically detect that it's JSON because you have Content-type: text/json, but it's a good idea to add it anyway.

$.get('getDataPartySize.php', function(data) {
      console.log(data);
}, 'json');

Or use $.getJSON:

$.getJSON('getDataPartySize.php', function(data) {
      console.log(data);
});
1

Try a for loop on the dataset:

for (var i=0; i<data.length; i++){
alert(data[i].name);
}
1

From what I can see, you are getting the JSON data properly, but you need to loop through and go through each object.

jQuery has a built in each method that would be useful for this:

$.get('getDataPartySize.php', function(data) {
    $.each(data, function(key, item){
        alert(item.name);
    });
},'json');

That would then alert the property 'name' for each object.

0

try $.getJSON() and see what you get

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.