JSON.parse意外的令牌多维数组
Hello I have simple question. I have some data fetched from database and json encoded and sent to html file over AJAX from PHP script. I want to JSON parse that data. Data looks like this:
And in PHP file like this:
$profesors = array
(
'id' => ($id),
'name' => ($profesor)
);
When I try to JSON parse it with JSON.parse() command I get "unexpected token {". Does anyone have some suggestion what to do? Also if it would be easier for achieving my goal I can use some other data type than array I am using, if someone knows better way...
EDIT
my PHP code that generates 2d array
<?php
$var=$_POST["oblast"];
$connection = mysql_connect('localhost','root','pass');
if (!$connection) {die("not successfull" . mysql_error());}
$result = mysql_query('set character set utf8', $connection);
$result = mysql_query('set names utf8', $connection);
$db_select = mysql_select_db("fakultet",$connection);
$brojac=0;
$profesor="";
//$data = array(array());
$queryData = mysql_query("SELECT * FROM predmet WHERE idpred = '$var'");
while($result1 = mysql_fetch_array($queryData))
{
$prof=$result1['idprof'];
$queryData1 = mysql_query("SELECT * FROM profesori WHERE idprof = '$prof'");
while($result2 = mysql_fetch_array($queryData1))
{
$id=$result2['idprof'];
$profesor=$result2['ime']." ".$result2['prezime'];
$profesors = array
(
'id' => ($id),
'name' => ($profesor)
);
echo json_encode($profesors);
}
//echo($result1['idprof']);
//$data[]=$result['idprof'];
}
//echo "$data";
?>
你好我有一个简单的问题。 我从数据库中获取了一些数据,json编码并通过PHP脚本从AJAX发送到html文件。 我想JSON解析那些数据。 数据如下所示: p>
p>
在这样的PHP文件中: p>
$ profesors = array
(
'id'=&gt;($ id),
'name'=&gt;($ profesor)
);
code> pre>
当我尝试JSON使用JSON.parse()命令解析它时,我得到“意外的令牌 {”。 有没有人有什么建议怎么办? 此外,如果它更容易实现我的目标,我可以使用一些其他数据类型而不是我正在使用的数组,如果有人知道更好的方式...... p>
编辑 p>
\ n
生成2d数组的PHP代码 p>
&lt;?php
$ var = $ _ POST [“oblast”];
$ connection = mysql_connect(' localhost','root','pass');
if(!$ connection){die(“not successfull”。mysql_error());}
$ result = mysql_query('set character set utf8',$ 连接);
$ result = mysql_query('set names utf8',$ connection);
$ db_select = mysql_select_db(“fakultet”,$ connection);
$ brojac = 0;
$ profesor =“”;
/ / $ data = array(array());
$ queryData = mysql_query(“SELECT * FROM predmet WHERE idpred ='$ var'”);
while($ result1 = mysql_fetch_array($ queryData))
{
$ prof = $ result1 ['idprof'];
$ queryData1 = mysql_query(“SELECT * FROM profesori WHERE idprof ='$ prof'”);
while($ result2 = mysql_fetch_array($ queryData1))
{\ n
$ id = $ result2 ['idprof'];
$ profesor = $ result2 ['ime']。“”。$ result2 ['prezime'];
$ profesors = array
(
' id'=&gt;($ id),
'name'=&gt;($ profesor)
);
echo json_encode($ profesors);
}
// echo($ result1 ['idprof']);
// $ data [] = $ result ['idprof'];
}
// echo“$ data”;
?&gt;
代码> pre>
div>
You are doing echo json_encode($profesors);
inside your while
loop. Don't do that. You should only ever call json_encode
once.
Try something like this:
$profesors = array();
while($result2 = mysql_fetch_array($queryData1))
{
$id=$result2['idprof'];
$profesor=$result2['ime']." ".$result2['prezime'];
$profesors[] = array(
'id' => $id,
'name' => $profesor
);
}
echo json_encode($profesors);
The unexpected token is the second opening curly brace. It's expecting one object because there are no array brackets. Either have your response look like this:
[{
"id" : "1",
"name" : "Ivan Nikolaj"
},
{
"id" : "2",
"name" : "Zdravko Topic"
}]
or this :
{
"id" : "1",
"name" : "Ivan Nikolaj"
}
or even better, this:
{
"status" : 200,
"response" : [
{
"id" : 1,
"name" : "Ivan Nikolaj"
},
{
"id" : 2,
"name" : "Zdravko Topic"
}
]
}