简体   繁体   English

JSON.parse意外令牌多维数组

[英]JSON.parse unexpected token multidimensional array

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. 我有一些从数据库中获取的数据和json编码,并通过PHP脚本通过AJAX发送到html文件。 I want to JSON parse that data. 我想用JSON解析该数据。 Data looks like this: 数据如下所示:

在此处输入图片说明

And in PHP file like this: 并在这样的PHP文件中:

$profesors = array
                (
                'id' => ($id),
                'name' => ($profesor)
                );

When I try to JSON parse it with JSON.parse() command I get "unexpected token {". 当我尝试使用JSON.parse()命令进行JSON解析时,出现“意外令牌{”。 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代码生成二维数组

<?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";
?>

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"
         }
     ]
 }

You are doing echo json_encode($profesors); 您正在做echo json_encode($profesors); inside your while loop. 您的while循环中。 Don't do that. 不要那样做 You should only ever call json_encode once . 您应该只调用一次 json_encode

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问json.parse多维数组php - accessing a json.parse multidimensional array php JSON.parse 单个变量 - Uncaught SyntaxError: Unexpected token &lt; in JSON - JSON.parse single variable - Uncaught SyntaxError: Unexpected token < in JSON JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: JSON JSON JSON.parse 0 中的意外标记 - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse SyntaxError:JSON中的意外令牌&lt;位于JSON.parse(位置0) <anonymous> )-AngularJS - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) - AngularJS 未捕获的语法错误:JSON 中的意外令牌 < position 中的 0 JSON.parse - Wordpress - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse - Wordpress Ionic + PHP:意外的令牌&lt;JSON中的JSON.parse位置0 - Ionic + PHP: Unexpected token < in JSON at position 0 at JSON.parse 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 从PHP制成的数组中使用JSON.parse时出现“意外令牌” - 'Unexpected token' when using JSON.parse from array made in PHP JSON.parse:意外的角色 - JSON.parse: unexpected character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM