PHP json_encode 函数返回空白 {}

标签 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 {}

最佳答案

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

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

关于PHP json_encode 函数返回空白 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530369/

相关文章:

PHP MYSQL LIKE 用于匹配描述中的多个单词

java - 使用 gcm 更新 ListView 中的 ImageView

json - 如何在 swift 4 中从多个字典写入多个 JSON 对象

iOS/Objective C 解析 JSON 获取 int

php - 如何安全地使用 LIKE

php - Jquery 自动完成与 mysql UTF8 不起作用

php - 填充编辑表单上的复选框

php - 如何从服务器获取JSon到ListView?

java - Jackson ObjectMapper 数组中的 JSON 空指针异常

ios - 无法使用 JSON 数据更新 UILabel