Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having trouble using the json object echoed from php to javascript. In the php file I define

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo($json);

and then in javascript file I want to access this object.

$("#test_btn").click(function() {
                $.get("serverside.php", function(data, status) { 
                   console.log("data " , data["a"]); //undefined
                   console.log(JSON.parse(data)); // error
                });
            });

I get undefined for data["a"] and an error for JSON.parse. How should I use the returend data?

share|improve this question
5  
Use an associative array and echo it with json_encode. Don't create your own JSON, it's prone to errors. –  elclanrs May 15 at 22:16
4  
Try just console.log(data) and see what it outputs. You're probably not getting valid JSON –  itsananderson May 15 at 22:20
1  
Which error are you getting? The error message usually helps. –  Felix Kling May 15 at 22:23
1  
I think I realized the problem, yet I don't know how to solve it! in the php file, I am reading the content of several json files, so I loop through all the files and echo in the loop. I thought the content of each file comes separately but accually what I get is a "{"a":1,"b":2,"c":3,"d":4,"e":5}{"a":1,"b":2,"c":3,"d":4,"e":5}{"a":1,"b":2,"c":‌​3,"d":4,"e":5}..." which is not valid json –  user1720860 May 15 at 22:26
1  
Read the contents of all your json files and put them in a PHP array with json_decode function. Once they're in a format you're happy with export them with json_encode. –  mjwatts May 15 at 22:56

4 Answers 4

up vote 3 down vote accepted

Based on your comment (echoing several json strings), you should do the following:

  1. Initialize an empty results array;
  2. Read your file and put it in an array or object using json_decode();
  3. Add this array / object to your results array;
  4. At the end, use json_encode() to encode and echo out your results array.
share|improve this answer
1  
Yes, that should work! thank you :) –  user1720860 May 15 at 22:32

You must make a JSON.parse(data) before attempting to access to data['a'], and send a header from PHP that implicitly say the browser that the data output is going to be a JSON.

header ('Content-Type: application/json');
share|improve this answer
2  
jQuery will make an intelligent guess and if it recognizes the json, it will be parsed already by the time you get it. –  jeroen May 15 at 22:25
3  
It doesn't matter to the order of JSON.parse(data) on success callback, as it actually trigger an error which indicates it just parsed invalid json. –  Rahil Wazir May 15 at 22:28

The problem might be that PHP is returning a string that looks like JSON.

In JS it might help to JSON.parse(data) to convert from string to JSON object, then you can access it.

$("#test_btn").click(function() {
  $.get("serverside.php", function(data, status) {
    $json = JSON.parse(data);
    console.log("data " , $json["a"]); // should now return 1
  });
});
share|improve this answer

you need to put json_encode or parse it in javascript

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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