1

I have created an AJAX function that is used with pagination links to load a new page of posts on the fly, and have everything working great except I can't figure out how to parse a JSON object like this one.

This is the style of PHP that is getting passed as JSON:

Array (
    [0] => stdClass Object (
        ['var1'] => val1
        ['var2'] => var2
    )
    [1] => stdClass Object (
        ['var1'] => val1
        ['var2'] => var2
    )
    [3] => stdClass Object (
        ['var1'] => val1
        ['var2'] => var2
    )
)

And is being passed to it with this:

$response = json_encode( array( 'success' => true, 'posts' => $new_posts ) );

Where $new_posts is the array I'm trying to parse.

Can anybody show me how I can access these variables and their values? Thanks.

Update: Here's the JSON string

I'm trying to access these from JavaScript.

Update 2: When I use var posts = JSON.parse( response.posts ); I get the following error in Google Chrome Javascript Console:

Uncaught SyntaxError: Unexpected end of input

Update 3: I just checked Firebug and it only seems to be returning this for the JSON response text:

{"success":true,"posts":[]}

3
  • 3
    Sooo.... do you feel like showing us the actual JSON? Commented Jan 23, 2012 at 2:18
  • Are you trying to access those variables from JavaScript? Commented Jan 23, 2012 at 2:20
  • Well the string is over 6500 characters long, so sorry for that! Updated my question. Commented Jan 23, 2012 at 2:22

3 Answers 3

2

I'm assuming you want to access via JavaScript since you have this in PHP. On your XHR success callback, insert the response in a JSON.parse(); and access everything as follows

var data = JSON.parse(recievedData); // newer browsers, optionally you can use jQuery or eval()
console.log(data); // test;
// loop through all data
for(var i=0;i<data.length;++i) {
   console.log(data[i].var1); // expects val1
   console.log(data[i].var2); // expects val2
}
//access one bit
console.log(data[0].var1);

jQuery response method (there are many ways to achieve this)

var data = jQuery.parseJSON(recievedData)
Sign up to request clarification or add additional context in comments.

Comments

1

Since you are using jQuery, the best approach would be to use the parseJSON method:

var respObj = jQuery.parseJSON(response);

3 Comments

When I use this, I get the following error in the Google Chrome Javascript console: Uncaught SyntaxError: Unexpected end of input - any idea why that would happen?
It most likely means that there is a un-terminated string in your JSON text. It might due to a string that is incorrectly escaped.
You were right, I had to double encode it and now I'm able to get the variables in my Javascript code. Thanks!
0

JSON is valid javascript, so the most straightforward solution (assuming you trust the source of the json 100%) is to simply eval() it.

var response = eval(the_json);

if (response.success){
    var posts = response.posts
}

If you're using a library like jQuery, there are built-in methods to more safely evaluate json strings.

4 Comments

I am using the jQuery library as well, the script is dependent on it. Can you show me how to do it that way?
You might also want to include a note about the new ECMAScript 5 standard window.json.
JSON.parse is more correct. If the JSON is coming from an untrusted source you're evaling their arbitrary javascript. Even in old versions of IE I used to just go grab the JSON class from json.org for a safer way of doing this.
Paul Irish also wrote a post that mentions the way jQuery parses JSON. Very informative.

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.