关于php:JSON资料后出现非预期的非空白字元?
2020-11-26
codeigniterjavascriptjqueryjsonphp
unexpected non-whitespace character after JSON
data?
我想在输出中将此PHP代码通过jquery.each()回显name,star_type,service,但是我遇到了错误。 如何解决?
错误:
An error has occured: [object Object] parsererror
SyntaxError: JSON.parse: unexpected non-whitespace character
after
JSON data
我有这个PHP代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//$hotel_id =
$this->input->post('hotel_id'); $hotel_id = array('1','2','3'); //print_r($hotel_id); foreach ($hotel_id as $val) {
$query_r = $this->db->query("SELECT * FROM hotel_submits WHERE id LIKE '$val'
ORDER BY id desc");
$data
= array();
foreach ($query_r->result() as $row) {
$data_s = json_decode($row->service, true);
$data_rp = json_decode($row->address, true);
$data[] = array(
'name' => $row->name,
'star_type' => $row->star . '-' . $row->type,
'site' => $row->site,
'service' => $data_s,
'address' => $row->address
);
}
echo json_encode($data);
}
|
这是在PHP代码上方输出的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[{
"name":"how",
"star_type":"5-hotel",
"site":"www.sasaas.assa",
"service": ["shalo","jikh","gjhd","saed","saff","fcds"]"address":"chara bia paeen"
}][{
"name":"hello",
"star_type":"4-motel",
"site":"www.sasasa.asas",
"service": ["koko","sili","solo","lilo"]"address":"haminja kilo nab"
}][{
"name":"hi",
"star_type":"3-apparteman",
"site":"www.saassaas.aas",
"service": ["tv","wan","hamam","kolas"],
"address":"ok"
}]
|
这是我的js代码出现错误:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
$.ajax({
type:"POST",
dataType:"json",
url: 'get_residence',
data: dataString_h,
cache: false,
success: function (respond) {
//;
$.each(respond[0].name, function (index, value) {
;
});
},
"error": function (x, y, z) {
alert("An error has occured:\
" + x +"\
" + y +"\
" + z);
}
});
|
您没有在回显有效的json。 试试这个:
1
2
3
4
5
6
|
$hotel_data = array();
foreach(...) {
// .. do stuff
$hotel_data[] = $data; // add $data to the end of the $hotel_data
array
} echo json_encode(array('data' => $hotel_data));
|
这会将所有$data数组包装为一个数组,并将其放入对象的data属性中。 您可以按以下方式在js端访问此数据:
1
2
3
|
$.each(response.data, function(i, obj) {
;
});
|
注意:我不确定我上面写的php语法,因为我写了php已经有一段时间了:)
您的JSOn完全无效。 您不应在循环内回显json-ecnoded数组,而应在循环外回显:
1
2
3
4
5
6
7
|
$all_data = array();
foreach ($hotel_id as $val) {
//..what you have there now, but instead if echo
json_encode($data); you do
$all_data[] = $data;
} //and finally echo json_encode('data'=>$all_data);
|
您的php输出不是有效的json,您在"address"之前错过了逗号。
您可以使用以下网址检查json:http://json.parser.online.fr/
https://www.codenong.com/7569192/
自己总结:
解决思路:利用PHP
foreach循环把‘结果集’转换成‘数组’,避开‘回显数数据’在前端用JSON.parse()转换时,会报上面错误。
错误的本质是返回的数据格式不符合JSON格式要求,个人感觉是因为数据中含有‘’空字符串引起的错误。
自己代码把返回的结果集$send打包成数组就成功的解决了这个报错问题。
$send = $stmt->fetchAll(PDO::FETCH_ASSOC);
//把结果集包装成数组,
//如果没有这个步骤,返回给前端的数据在JSON.parse()会有 ‘unexpected non-whitespace character after JSON data’的错误提示。解决思路:把获取的结果集打包成数组。
//参考地址:https://www.codenong.com/7569192/
$send_arr=array();//申明一个空数组
foreach ($send as $k=>$v){//把结果集通过foreach打包到成数组。
$send_arr[$k]=$v;
}
echo json_encode($send_arr,JSON_D_UNICODE);
加载中,请稍候......