这是一个Array,Object还是只是一个看起来像Json并从PHP输出的String?

时间:2011-05-27 11:54:52

标签: php jquery json

我在浏览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个elmenets,但是只有大约50个帖子,所以它将每个字符串从这个字符串中作为数组元素。

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上尝试了很多方法..但我觉得我错过了什么..

5 个答案:

答案 0 :(得分:3)

您在“{}” - 块

之间缺少逗号
[{"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"},{}]

答案 1 :(得分:1)

对@JochenJung的反应,你需要这样:

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

答案 2 :(得分:1)

您的问题是您正在一起输出多个JSON对象。

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

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

如果你想让Post个对象输出他们的JSON对象数组,那么你也需要为它编写JSON代码。

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

你可以这样做:

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

而不是foreach()循环。

答案 3 :(得分:0)

答案 4 :(得分:0)

尝试做

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


            }});

1.从所有文件中删除所有空白区域。(需要清洁)
2.使用json_encode('这里可以是阵列);

相关问题