这是一个数组、对象还是一个看起来像Json并从PHP输出的字符串


is this an Array, Object or only a String that Looks like Json and outputted from PHP?

我在读取jQuery中的JSON数据时遇到问题,该数据由PHP输出,我已经在使用json_encode();

<?php
require_once 'Post_db.php'; // a Class for doing MySQL work
/* the Post class */    
class Post {
/* An array that stores the post item data: */
private $pdata;
/* The constructor */
public function __construct($postData){
if(is_array($postData))
$this->pdata= $postData;
}

public function __toString(){
// The string that is beeing returned
// is outputed by the echo statement
$toBeJsoned=$this->pdata;
$toBeSent=json_encode($toBeJsoned);
return  $toBeSent;  
}
//read the Posts
public static function readPosts(){
$query = new Post_db("SELECT * FROM pmessage
                 ORDER BY p_id DESC
                 ");
$posts = array();
while($row = $query->fetchRow()){
$posts [] = new Post($row);
}
foreach($posts as $item){
echo $item;
}
}

现在Jquery:

此代码正在读取,但这只是一个$.get方法。它将myPosts返回为1个数组,其中包含大约5000个Elmenet,但只有大约50个Posts,因此它将从该字符串中获取每个字符作为一个数组元素。

function readMyPosts(){
        $.get("phpFileThatCalltheFunctionsToRead.php",
                      {"action": "autoRead"},
                      function(myPosts){        
            //a ul element
            $("#plist").html("<li>"+myPosts+"</li>");       
               });
                 }
    readMyFacts();

当我尝试使用$.ajax$.getJSON时,我从未达到成功函数,实际上没有发生任何事情

  function ReadNewestFacts(){
        $.ajax({
        url:"phpFileThatCalltheFunctionsToRead.php", 
        data:"action=autoRead_main",
        dataType:"json",
        success: function(json){
            //do something with the data 
            alert(json);

            }});
      }
   //run this function
        ReadNewestFacts();

phpFileThatCalltheFunctionsToRead.php文件如下所示:

<?php
session_start();
require_once "post.class.php";

 if(isset($thisUid)){
$thisUid= $_GET['thisUid'];
}
if(isset($action)){
$action = $_GET['action'];
}
try{
    switch($_GET['action'])
    {
        case 'autoRead_main':
            Post::readPosts();
            break;      
        case 'autoRead':
            Post::readMyPosts($_GET['thisUid']);
            break;
            case ' more cases
               ..
                  .
                .
              .'


    }
}
catch(Exception $e){
    // echo $e->getMessage();
    die("0");
}

我能收到的只有

{"p_id":"1","p_text":"blabla","p_adder":"1"}{"p_id":"2","p_text":"blabla2","p_adder":"1"}{"p_id":"3","p_text":"more blabla","p_adder":"2"}{"p_id":"4","p_text":"more and more blabla","p_adder":"1"}{}....

JSON格式似乎工作正常!?,,但我认为Jquery无法从PHP中读取JSON字符串?我真的不确定:(..

我无法访问jQuery端的JSON数据。。我在StacKover上尝试了很多方法。。但我想我错过了什么。。

"{}"-块之间缺少逗号

[{"p_id":"1","p_text":"blabla","p_adder":"1"},{"p_id":"2","p_text":"blabla2","p_adder":"1"},{"p_id":"3","p_text":"more blabla","p_adder":"2"},{"p_id":"4","p_text":"more and more blabla","p_adder":"1"},{}]

您的问题是同时输出多个JSON对象。

这是因为PHP代码中的每个Post对象都输出一个JSON块。您正在创建一个Post对象数组,因此您得到了多个JSON块。

您生成的每个JSON块都是有效的,但简单地将它们连接在一起是无效的JSON。

如果您想让Post对象输出其JSON对象的数组,那么您还需要为此编写JSON代码。

你不能json_encode,因为它已经编码了,所以在代码中修复它最简单的方法就是在输出的两端打印[],并在每个Post对象输出之间打印一个逗号。

你可以这样做:

echo '['.implode(',',$posts).']';

而不是CCD_ 14循环。

为了回应@JochenJung,你需要这样做:

json_encode( array( array1, array2, ...) );插入逗号。

使用json_encode()json_decode()

尝试执行

error: function(json){
            console.log(json);//need to have firebug

            }});

1.从所有文件中删除所有空格。(需要清洁)
2.使用json_encode('HERE CAN BE ARRAY);