简体   繁体   English

PHP json_encode与JAVASCRIPT JSON.parse

[英]PHP json_encode versus JAVASCRIPT JSON.parse

If I create in PHP a json like this: 如果我在PHP中创建这样的json:

          if ( ($result = mysqli_query($link, $sql))  && (mysqli_affected_rows($link)!==0) )  {
                 $entries = array();
                 while ($row = mysqli_fetch_assoc($result)) {
                        $entries[] = $row;
          }
          $data = json_encode($entries);
          echo($data);

I will get this result: 我会得到以下结果:

[   
    {"id":"100043","title":"Mini for Sale","session":"1407456000","totalViews":"0"},
    {"id":"100000","title":"test","session":"1408366541","totalViews":"4"},
    {"id":"100001","title":"Le Cappa","session":"1408377143","totalViews":"0"},
    {"id":"100002","title":"Le Cappa","session":"1408378069","totalViews":"0"},
    {"id":"100003","title":"test","session":"1408378833","totalViews":"0"}
]

If I do this with JavaScript: console.log("jsondata: ", JSON.parse(data)); 如果我使用JavaScript执行此操作:console.log(“ jsondata:”,JSON.parse(data));

(where data is the json above: data = [{"id ... ) (其中data是上面的json:data = [{“ id ...]

I will get this result: 我会得到以下结果:

jsondata: [
 Object { id="100043", title="Mini for Sale", session="1407456000", mehr...},
 Object { id="100000", title="test", session="1408366541", mehr...},
 Object { id="100001", title="Le Cappa", session="1408377143", mehr...},
 Object { id="100002", title="Le Cappa", session="1408378069", mehr...},
 Object { id="100003", title="test", session="1408378833", mehr...}]

My question: Why is this different and how can I get with PHP a json with objects like the javascript one? 我的问题:这有何不同?如何用PHP获得带有javascript等对象的json?

Some more Information: I'm trying to implement a table with the dynatable plugin. 更多信息:我正在尝试使用dynatable插件实现一个表。 It works only if I pass the Data sended by PHP again with JSON.parse. 仅当我再次使用JSON.parse传递PHP发送的数据时,此方法才有效。 Thats means to me that the PHP-json is wrong. 那对我来说意味着PHP-json是错误的。

 $.ajax({
  url: 'http://huntinggrounds.de/stats/test.php',
  success: function(data){  console.log("data: ",data);     console.log("jsondata: ", JSON.parse(data));
    $('#my-final-table').dynatable({
      dataset: {
        records: JSON.parse(data)
      }
    });
  }
});

Here are booths results copied from console. 以下是从控制台复制的展位结果。

data: [{"id":"100043","title":"Mini for Sale","session":"1407456000","totalViews":"0"},{"id":"100000","title":"test","session":"1408366541","totalViews":"4"},{"id":"100001","title":"Le Cappa | Franco Gravante","session":"1408377143","totalViews":"0"},{"id":"100002","title":"Le Cappa | Franco Gravante","session":"1408378069","totalViews":"0"},{"id":"100003","title":"test","session":"1408378833","totalViews":"0"}]

jsondata: [Object { id="100043", title="Mini for Sale", session="1407456000", mehr...}, Object { id="100000", title="test", session="1408366541", mehr...}, Object { id="100001", title="Le Cappa | Franco Gravante", session="1408377143", mehr...}, Object { id="100002", title="Le Cappa | Franco Gravante", session="1408378069", mehr...}, Object { id="100003", title="test", session="1408378833", mehr...}]

What you're seeing in the console is an interactive debug representation of an actual Javascript object in memory. 您在控制台中看到的是内存中实际Javascript对象的交互式调试表示。 Is is not JSON. 是不是JSON。 Your PHP output already is the perfect JSON representation of that Javascript object. 您的PHP输出已经是该Javascript对象的完美JSON表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM