0

I'm trying to parse a JSON data structure but what I think should be data is returning undefined.

Here is the jQuery I'm using:

....
var messages = data.messages;

$.each(messages, function(i, val) {

    var user = messages[i];

    //console.log(user);
    console.log(user['msg']);

});

The PHP data structure look like this:

...
$message_info = array();

$message_info[$row['username']]['prvt'] = 1;
$message_info[$row['username']]['msg'] = stripslashes($row['message']);
$message_info[$row['username']]['ts'] = $row['timestamp'];

...

$message_list[] = $message_info;

...

$res->messages = $message_list;
echo json_encode($res);

If I dump user to the console the output look like this:Object

{john: Object}
  john: Object
  msg: "test msg"
  prvt: 0
  ts: "2012-12-10 09:16:13"

This is what data looks like in the console:

Object {success: true, lastid: "60", messages: Array[15]}
lastid: "60"
messages: Array[15]
  0: Object
    john: Object
      msg: "test msg"
      prvt: 0
      ts: "2012-12-10 09:16:13"
  1: Object
    john2: Object
      msg: "test msg2"
      prvt: 1
      ts: "2012-12-10 09:18:13"
 ...

Any idea why I can't access and retrieve the contents of msg?

5
  • try with echo json_encode($message_list,JSON_FORCE_OBJECT); Commented Dec 10, 2012 at 20:39
  • 1
    Are you sure it's not user.john.msg ? Commented Dec 10, 2012 at 20:40
  • 1
    And if the above doesn't work a console.log(data) right after you get the data variable might help. Note also that you don't need user = messages[i] because the val parameter will already be set to messages[i]. Commented Dec 10, 2012 at 20:40
  • Are you getting this JSON using AJAX? Are you trying to access the data before the AJAX callback is complete? Commented Dec 10, 2012 at 20:44
  • @nnnnnn Just added output from data Commented Dec 10, 2012 at 20:49

2 Answers 2

1

Rewrite your PHP to avoid nesting msg inside of $row['username']:

$message_info = array();
$message_info['username'] = $row['username']; // find this in JS with user['username']
$message_info['prvt'] = 1;
$message_info['msg'] = stripslashes($row['message']);
$message_info['ts'] = $row['timestamp'];

With that change, your JavaScript should work as-is.

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

3 Comments

That worked but I still don't understand why I couldn't reference the username as the key...
Because the username is different each time, making it impossible for you to write code to access the data underneath it. If you want to user a username to locate data, loop through the array and test if user['username'] equals a given string.
Thanks, I guess I was making more difficult then it had to be. Thanks again!
0

your user objects contains an object that is referenced by the key "john", "john2" etc.

user.john.msg or user.john2.msg

should work.

Therefore to build a more generic json structure in php, i would do the following:

...
$message_info['user']['name'] = $row['username'];
$message_info['user']['prvt'] = 1;
...

1 Comment

The username could be anything so I can make a static call like that

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.