【问题标题】:Remove double quote in json_encode删除 json_encode 中的双引号
【发布时间】:2019-02-15 07:38:36
【问题描述】:

我从我的数据库中提取了一些数据。工作完美。 但我想删除 JSON 中的双引号。 这是我的代码;

$sql = "SELECT id, instructions, quiz_question, correct, wrong, wrong1, wrong2 FROM student_quiz WHERE subject = 'SOCIAL STUDIES' AND type = 'challenge'";

$results = $pdo->query($sql);
$results->setFetchMode(PDO::FETCH_ASSOC);

$json = [];

while($row = $results->fetch()) {
    $choices = [
        $row['correct'],
        $row['wrong'],
        $row['wrong1'],
        $row['wrong2'],
    ];

    // shuffle the current choices so the 1st item is not always obviously correct
    shuffle($choices);

    $json[] = [
        'question' => $row['quiz_question'],
        'choices' => $choices,
        'correctAnswer' => $row['correct'],
    ];
}

echo json_encode($json);

正在回显这样的数据;

{"question":"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>","choices":["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],"correctAnswer":"Kwame Nkrumah"}

但我想要这样:

{question:"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>",choices :["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],correctAnswer :"Kwame Nkrumah"}

【问题讨论】:

  • JSON 要求属性名称用双引号引起来。你为什么要改变它?你想达到什么目的?
  • 他说了什么,你不希望这样,因为你无法使用json_decode 对其进行解码,事实上我为另一个问题写了一个解析器,因为他们有 json没有他们无法使用内置函数解析的引号......只是说。
  • 您想要的格式称为“JSOL”(JavaScript 对象文字)。如前所述,在 PHP 领域几乎没有解析器和生成器。并且通常应该避免,除非有比“我想要”更好的理由。
  • @Robee 双引号非常适合 JavaScript 对象文字表示法。你不需要摆脱它们。
  • Javascript 应该仍然可以使用引号。 JSON.parse()

标签: javascript php json


【解决方案1】:

你可以尝试像这样编码然后解码

$encodeJson = json_encode($json);

print_r(json_decode($encodeJson, true));

如果您想要每个偏移量,那么您可以像这样打印它,并且不会添加引号:

$decodeJson = json_decode($encodeJson, true);
print $decodeJson['question'];

编辑:下面的代码是评论答案:

$data = array(
"question" => "Who said this statement Ghana your beloved country is free for ever",
"choices" => array("Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"),
"correctAnswer" => "Kwame Nkrumah"
);
$jsonEncode = json_encode($data);
$jsDecode = json_decode($jsonEncode, true);

// a correct json encoded array would output something like this:

{
"question":"Who said this statement Ghana your beloved country is free for ever ?",
"choices":[
    "Jack Chan",
    "Donald Trump",
    "Ato Ahoi",
    "Kwame Nkrumah"
],
"correctAnswer":"Kwame Nkrumah"
}

//to select correct answer
print $jsDecode['correctAnswer'];

【讨论】:

  • 这不会产生 OP 想要的东西。
  • 请问如何获得正确答案的索引?说:'correctAnswer'=>2 和动态的。
  • @Robee 我已经更新了我的答案,请注意我对初始 json 输出的更改,因为您的数据的 json 格式无效。
  • 以前的代码有效。我认为javascript函数无法读取,但确实如此。谢谢@JamesBond
  • @Robee 真棒,如果我的回答是对的。您能否投票并将其标记为正确:
【解决方案2】:

PHP 给出的输出是正确的,如果你需要这个输出:

{question:"Who said this statement \"Ghana your beloved country is free for ever\"?
<\/p>",choices :["Jack Chan","Donald Trump","Ato Ahoi","Kwame Nkrumah"],correctAnswer :"Kwame Nkrumah"}

在javascript中,尝试使用..

var json = JSON.parse(response);

在php中尝试使用

$json = json_decode($json);

希望这会有所帮助。

【讨论】:

  • 解析 JSON 不会改变原始字符串,而且 OP 也不需要这样做。
  • 是的,兄弟。还要确保包含 header('content-type: application/json');
  • 这并不能真正回答问题,因为var json 不再是 php 字符串,甚至不是 javascript 字符串。
猜你喜欢
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2017-11-20
相关资源
最近更新 更多