我从MySQL获取数据它工作正常并转换为JSON中的数据但问题是如果有任何带撇号的字符,则它不会显示该数据。
这是我的代码
<?php
$con = mysql_connect("test.db.6420177.hostedresource.com","test","test");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bassessment", $con);
$AssessmentID=$_GET['AssessmentID'];
$query = "SELECT * from AssessmentResponsesMaster Ar LEFT JOIN AssessmentStandardQuestion Aq ON Aq.QuestionID=Ar.QuestionID where Ar.AssessmentID='$AssessmentID' AND Ar.SectionName ='12' ";
$res = mysql_query($query,$con) or die('$query gave error: ' . mysql_error());
if (!function_exists('json_encode'))
{
function json_encode($a=false)
{
if (is_null($a)) return 'null';
if ($a === false) return 'false';
if ($a === true) return 'true';
if (is_scalar($a))
{
if (is_float($a))
{
// Always use "." for floats.
return floatval(str_replace(",", ".", strval($a)));
}
if (is_string($a))
{
static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
}
else
return $a;
}
$isList = true;
for ($i = 0, reset($a); $i < count($a); $i++, next($a))
{
if (key($a) !== $i)
{
$isList = false;
break;
}
}
$result = array();
if ($isList)
{
foreach ($a as $v) $result[] = json_encode($v);
return '[' . join(',', $result) . ']';
}
else
{
foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
return '{' . join(',', $result) . '}';
}
}
}
$rows = array();
while($row = mysql_fetch_assoc($res)) {
$rows[] = $row;
}
echo json_encode($rows);
?>
示例JSON数据,其中显示QuestionText null,其中有一个撇号:
[{"ResponseID":"5157","AssessmentID":"9","QuestionID":"112","ResponseText":"Yes","Assessment Name":"Summit","AssessmentDate":"28-Jun-2014 11:14 AM","AssessmentQuestion":null,"ResponseComment":" ","DepartmentID":"15","SectionName":"12","QuestionText":"Make eye contact, smile and greet the guest or employee immediately."},
{"ResponseID":"5158","AssessmentID":"9","QuestionID":"113","ResponseText":"na","AssessmentName":"Summit","AssessmentDate":"28-Jun-2014 11:14 AM","AssessmentQuestion":null,"ResponseComment":" " ,"DepartmentID":"15","SectionName":"12","QuestionText":null}
]
答案 0 :(得分:0)
不要创建自己的功能。这只是一个小例子
<?php
$array[0] = "I'm";
$array[1] = 'Conf"used';
echo json_encode($array);
//OR
echo json_encode($array,JSON_HEX_APOS);
?>
输出= [“我糊涂了”] 第二输出 [“我是”,“Conf \”使用“]
答案 1 :(得分:0)
您可以使用json_encode函数。哪个是php库函数。 请查看此功能的手册。 Click Here。