I am creating a JSON object in PHP like this:
echo json_encode(array("results" => array(array("user" => $member['user']),array("company" => $member['company']))));
In the JavaScript I get something like:
"{"results":[{"user":"David"},{"company":"something"}]}"
Then I try to validate this JSON and it is not valid, but when I remove double quotes at the beginning and at the end then it is validate JSON.
What am I doing wrong? This is how it should be:
{"results":[{"user":"David"},{"company":"something"}]}
EDIT:
part of my AJAX call:
success: function(response)
{
for(var i=0;i<response.results.length;i++)
{
sessionStorage.setItem('user',response.results[i].user);
sessionStorage.setItem('company',response.results[i].company);
}
}
You appear to be double-encoding it. Either that, or you're encoding it and then dumping it inside quotes.
To be clear, you should have something like this:
var myJSobject = <?php echo json_encode(...); ?>;
Then it's good to go, nothing else needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With