Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除json_encode中的双引号_Javascript_Php_Json - Fatal编程技术网

Javascript 删除json_encode中的双引号

Javascript 删除json_encode中的双引号,javascript,php,json,Javascript,Php,Json,我从数据库中提取了一些数据。它工作得很好。 但是我想删除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->se

我从数据库中提取了一些数据。它工作得很好。 但是我想删除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"}
{“问题”:“谁说过这句话\“加纳,你们心爱的国家永远自由\”?
“,”选择“:[“Jack Chan”,“Donald Trump”,“Ato Ahoi”,“Kwame Nkrumah”],”正确答案“:“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"}
{问题:“谁说了这句话\“加纳,你心爱的国家永远自由\”?
,选项:[“Jack Chan”、“Donald Trump”、“Ato Ahoi”、“Kwame Nkrumah”],正确答案:“Kwame Nkrumah”}

如果您需要此输出,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"}
在php中尝试使用

$json = json_decode($json);

希望这有帮助。

如果您需要此输出,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"}
在php中尝试使用

$json = json_decode($json);

希望这有帮助。

您可以尝试这样编码然后解码

$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'];

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

$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'];


JSON要求属性名用双引号引起来。你为什么要改变这一点?你想实现什么?他说了什么,你不想这样,因为你不能用
json\u decode
来解码它,事实上,我前阵子为另一个问题写了一个解析器,因为他们的json没有引号,他们无法用内置函数解析这些引号。。。你想要的格式叫做“JSOL”(JavaScript对象文本)。正如前面提到的,在PHP领域中很少有解析器和生成器。通常应该避免使用双引号,除非有比“我想要”更好的理由。@robe双引号对于JavaScript对象文字符号来说非常合适。你不需要去掉它们,Javascript仍然可以使用引号。JSON.parse()JSON要求属性名用双引号引起来。你为什么要改变这一点?你想实现什么?他说了什么,你不想这样,因为你不能用
json\u decode
来解码它,事实上,我前阵子为另一个问题写了一个解析器,因为他们的json没有引号,他们无法用内置函数解析这些引号。。。你想要的格式叫做“JSOL”(JavaScript对象文本)。正如前面提到的,在PHP领域中很少有解析器和生成器。通常应该避免使用双引号,除非有比“我想要”更好的理由。@robe双引号对于JavaScript对象文字符号来说非常合适。你不需要去掉它们,Javascript仍然可以使用引号。解析JSON不会改变原始字符串,OP也不需要这样做。还要确保包含标题('content-type:application/json');这并不能真正回答问题,因为
var json
不再是php字符串,甚至不是javascript字符串。解析json不会改变原始字符串,OP也不需要这样做。Yea bro。还要确保包含标题('content-type:application/json');这并不能真正回答问题,因为
var json
不再是php字符串,甚至不是javascript字符串。这不会产生OP想要的结果。请您如何获得正确答案的索引?说:“correctAnswer'=>2和dynamic。@Robee我已经更新了我的答案,请注意我对初始json输出的更改,因为您的数据格式对json无效。以前的代码有效。我以为javascript函数无法读取,但它确实读取了。谢谢@JamesBond@Robee太棒了,如果我的答案是正确的。你能投票并将其标记为正确的吗:这并不能产生OP想要的结果。请你如何获得正确答案的索引?说:“correctAnswer'=>2和dynamic。@Robee我已经更新了我的答案,请注意我对初始json输出的更改,因为您的数据格式对json无效。以前的代码有效。我以为javascript函数无法读取,但它确实读取了。谢谢@JamesBond@Robee太棒了,如果我的答案是正确的。请您投票并将其标记为正确: