I am creating a JSON object in PHP like this:
echo json_encode(array("results" => array(array("user" => $member['user']),array("pany" => $member['pany']))));
In the JavaScript I get something like:
"{"results":[{"user":"David"},{"pany":"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"},{"pany":"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('pany',response.results[i]pany);
}
}
I am creating a JSON object in PHP like this:
echo json_encode(array("results" => array(array("user" => $member['user']),array("pany" => $member['pany']))));
In the JavaScript I get something like:
"{"results":[{"user":"David"},{"pany":"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"},{"pany":"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('pany',response.results[i].pany);
}
}
Share
Improve this question
edited Aug 15, 2013 at 10:51
user123_456
asked Aug 15, 2013 at 9:18
user123_456user123_456
5,82526 gold badges87 silver badges142 bronze badges
6
- 2 How are you "trying to validate" the json? Also where do you get that json string from? – PeeHaa Commented Aug 15, 2013 at 9:20
- How your javascript code look like? – jcubic Commented Aug 15, 2013 at 9:21
- this should not be a problem if you echo javascript code (btw JSON stands for JavaScript Object Notation) with php, so obviously you are doing something wrong. Post your js code that uses this php echoed json. – Ivan Hušnjak Commented Aug 15, 2013 at 9:22
-
1
How are you constructing the JS? Did you accidentally used
var json="<?php echo json_encode(...);?>";
? – Passerby Commented Aug 15, 2013 at 9:22 - I tested your code above and the result is like the result you want. Maybe you should show some more code like in the side of the javascript. – shark Commented Aug 15, 2013 at 9:23
3 Answers
Reset to default 9You 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.
echo json_encode(
array(
"results" =>
array(
array("user" => $member['user'], "pany" => $member['pany'] ),
array("user" => $member['user2'], "pany" => $member['pany2'] )
)
)
);
It seems like ur copying the string with the quotes from somewhere (from a log or something?), and trying to validate somewhere else. echo json_encode(..)
should give u the correct Json string!