1

i am using this to get informations from ajax jquery in json formate but it give me error like this 0 =[object object]

$.getJSON("ajax_files/getSingleRow.php?id="+id+"&type="+type, function(json){ 
 $.each(json, function(key, val) {
 //$("#"+key).val(val);
 alert(key+'='+val);
 });
 });

here is my josn string

[{"id":"1","ref":"RH-R-1","name":"","description_demo":"this is desc test"}]

Thanks all...here is how my json develops

 while($rw = $oAppl->row($res))
{
 $return[]=array('id'=>$rw['id'],
 'ref'=>$rw['ref'],
 'name'=>$rw['name'],
 'description_demo'=>$rw['description_demo']);
}
 header('Content-type: application/json');
 echo json_encode($return);
casperOne
74.7k19 gold badges189 silver badges262 bronze badges
asked Mar 4, 2013 at 12:41
4
  • Why do you think that is an error? Commented Mar 4, 2013 at 12:43
  • Your json is an Array with one element that is an object. Commented Mar 4, 2013 at 12:46
  • Do alert(json) to see the data you received?? Commented Mar 4, 2013 at 12:47
  • @asifsid88 The alert() function is awful for debugging and displaying information, as they've just discovered, because it doesn't show the properties of objects. Commented Mar 4, 2013 at 12:50

5 Answers 5

2

The JSON text consists of an array containing an object.

When you loop over it, you get the first key of the array (0) and then the string serialisation of the object ([Object object]). Then it stops because there is only one entry in the array.

This is not an error. It is expected behaviour given the data you are inputting.

Possibly you want to loop over the object instead, in which case:

var ob = json[0];
$.each(ob, function(key, val) {
answered Mar 4, 2013 at 12:44

Comments

0

That isn't an error, strictly, it's an object. It might contain an error, who knows. But the thing itself is a thing waiting to be accessed (i.e., the value of val) and there's nothing inherently erroneous with it.

Try accessing the members, output the id, name, etc, instead of just dumping the thing in an alert.

answered Mar 4, 2013 at 12:43

Comments

0

Try this:

$.getJSON("ajax_files/getSingleRow.php?id=" + id + "&type=" + type, function (json) {
 $.each(json, function (key, val) {
 // This will only give you the 'ref' value
 alert(key + '=' + val.ref);
 // To loop through all the values
 $.each(val, function (key2, data) {
 alert(key2 + ": " + data);
 });
 });
});

DEMO HERE

answered Mar 4, 2013 at 12:45

Comments

0

Your JSON string is wrapped inside an array. [ {} ] Leave the "[" and "]" out.

answered Mar 4, 2013 at 12:46

Comments

0

Read the propery value of your JSON collection item.

 $.each(json, function(key, item) {
 alert(key+'='+item.id);
 alert(key+'='+item.ref);
 alert(key+'='+item.description_demo);
 });

This should work fine assuming you have no other script errors in your page.

Working sample : http://jsfiddle.net/a4Efx/4/

answered Mar 4, 2013 at 12:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.