-1

If I create in PHP a json like this:

          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));

(where data is the json above: 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?

Some more Information: I'm trying to implement a table with the dynatable plugin. It works only if I pass the Data sended by PHP again with JSON.parse. Thats means to me that the PHP-json is wrong.

 $.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...}]
4
  • $data is the encoded json string; you need to json_decode it if you want to get objects. Commented Aug 23, 2014 at 9:29
  • I'll take a guess for the reason of the downvote: it's kind of a facepalm question, really. :) Commented Aug 23, 2014 at 10:13
  • sorry for the facepalm question. But I didn't get that I have to pass a json string first to an json object to use it. Commented Aug 23, 2014 at 10:24
  • 1
    Not a "json object", there's no such thing! There is Javascript, in which you can write code which includes things like objects or arrays. And there's JSON, which is an abbreviation for JavaScript Object Notation, which is a text format like XML or YAML or such to describe object and array structures as strings. The syntax of JSON is borrowed from Javascript, because it turns out it's pretty nice syntax for this purpose. You parse a JSON string into a Javascript object (or a PHP array for that matter). Substitute JSON for XML and the difference should be obvious. Commented Aug 23, 2014 at 10:38

1 Answer 1

2

What you're seeing in the console is an interactive debug representation of an actual Javascript object in memory. Is is not JSON. Your PHP output already is the perfect JSON representation of that Javascript object.

7
  • but they are different. why? if the PHP-output is the perfect JSON representation why it do not work? Commented Aug 23, 2014 at 9:56
  • @hamburger — I can't see any differences in the data. The only differences I can spot are in how the different tools visualise it. Commented Aug 23, 2014 at 9:57
  • 1
    @hamburger Because JSON is a text format in which you can describe complex nested data structures, and send them over the wire as text to another system. That system then parses this text format into an internal data structure. What you see in the console is a representation of that parsed data structure, you're looking at an actual Javascript object. JSON is not the same as a Javascript object. You're already doing everything correctly. It just looks different, that's all. Commented Aug 23, 2014 at 9:59
  • different tools visualise it? Isn't it always the same tool console.log? Commented Aug 23, 2014 at 10:00
  • @deceze Why do I have to parse the PHP-json again to JSON.parse? Commented Aug 23, 2014 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.