1

I have some PHP that generates the a JSON object from data from a MySQL database

$addressData = mysql_query("SELECT * FROM address WHERE ContactID = $contactID")or  die("<br/><br/>".mysql_error());

while($r = mysql_fetch_assoc($addressData)){
    $rows[] = array('data' => $r);
}

// now all the rows have been fetched, it can be encoded
echo json_encode($rows);

This generates the following JSON object:

[
  {"address":
    {"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"}},
  {"address":
    {"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}}
]

When receiving it back in Ajax and running it through the following:

$.each(data, function(key, val) {
  string =string + "Key: " + key + " Value:" + val + "<br />";
});

which prints the following:

Key: 0 Value:[object Object]

Key: 1 Value:[object Object]

Any ideas about how I can access the objects within key 0 and 1 in data?

2 Answers 2

2

This is because Object's default toString implementation results in "[object Object]" when concatenating to a String object. You can access the fields of the val object as usual, for example:

val.data.AddressID

like this:

$.each(data, function(key, val) {
  string += "Key: " + key + " Value:" + val.data.AddressID + "<br />";
});

Please note that val in the loop above is the {"data": ...} part of the JSON code, that is why you need to specify val.data. to access parts of the address data inside.

Also, you could simply remove the data nesting level, resulting in a JSON layout like that:

[
    {"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"},
    {"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}
]

Using this PHP code:

while($r = mysql_fetch_assoc($addressData)){
    $rows[] = $r;
}

And then, you could access the address data fields in the loop like this:

$.each(data, function(key, address) {
  string += "Key: " + key + " Value:" + address.AddressID + "<br />";
});
Sign up to request clarification or add additional context in comments.

2 Comments

no because val is the {data: {}} object. he should change the format of his json and get rid of the data: nesting
Agree, the data field's nesting level is useless, I will add that to the answer.
1

i would change the layout of your json, you don't need the nesting. try:

[
    {"AddressID":"10011","AddressType":"Delivery",...},
]

then you can iterate the return value as an array with key => val where val will be the object, because right now, val is the object holding the object you want...

1 Comment

Good advice. Just change $rows[] = array('data' => $r); to $rows[] = $r; and your old code will work fine.

Your Answer

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.