PHP json_encode函数返回空白{}

时间:2013-12-11 21:34:42

标签: php json

我正在尝试使用AJAX调用来获取一些数据,并通过JSON将多个数据返回给调用者。它适用于包含简单输出的某些测试。但是当返回的其中一个元素是HTML时,它就不起作用了。有什么想法吗?

// get_answer() pulls some HTML back from an XML document
$answer = $_SESSION['quiz_session']->get_answer();

// test output to make sure everything is working
echo $answer;

/** sample output **
 *     
      <div>
        <p>
            <b>
                <span class="gloss-def">a downward slope</span>
            </b>
        </p>
        <p>Because the village was situated on the 
            <i>declivity</i> of a hill, it never flooded.
        </p>
        <p>
            <i>Synonyms: decline; descent; grade; slant; tilt</i>
        </p>
      </div>
 *
 ** end sample output **/

 echo json_encode($answer);
 // will output {}

1 个答案:

答案 0 :(得分:2)

尝试添加$ answer和stdClass或数组,例如

echo json_encode(array("answer"=>$answer));