最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - json_encode adds double quotes when parsed - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 1 more ment

3 Answers 3

Reset to default 9

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.

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!

发布评论

评论列表(0)

  1. 暂无评论