Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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
处理php和jQuery中的json变量,改为使用文本变量_Php_Jquery_Ajax_Json - Fatal编程技术网

处理php和jQuery中的json变量,改为使用文本变量

处理php和jQuery中的json变量,改为使用文本变量,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我使用jquery通过ajax从php脚本接收文本,如下所示: $.ajax({ dataType: 'json', url: 'someURL.com', success:function(result){ json = JSON.parse(result.rgraph); }) result.rgraph在php中发送为: $arr = array( 'actiondata'=&g

我使用jquery通过ajax从php脚本接收文本,如下所示:

$.ajax({
        dataType: 'json',
           url: 'someURL.com',
                success:function(result){
                    json = JSON.parse(result.rgraph);
})
result.rgraph在php中发送为:

$arr = array( 'actiondata'=> 'some info here',  'rgraph' => $this->_rgraph() );
$jsonTable = json_encode($arr);

return $jsonTable;
其中$this->rgraph()只是一个函数,返回:

返回“{id:“0_0”,名称:“Categorie”,子项:[{id:“0_1”,名称:“商业服务”,数据:{relation:”},子项:[{id:“0_2”,名称:“H-FARM Ventures”,数据:{relation:”,子项:[},{id:“0_3”,名称:“原因”,数据:{relation:”},子项:[{id:“0_4”,名称:“StartupVisa”,数据:{relation:”,子项:[},{id:“0_5”,名称:“社区”,数据:{relationship:”},儿童:[{id:“0_6”,名称:“不同的解决方案”,数据:{relationship:”},儿童:[]},{id:“0_7”,名称:“新闻/媒体网站”,数据:{relationship:”,儿童:[{id:“0_8”,名称:“iSpazio”,数据:{relationship:”,儿童:[]},{id:“0_9”,名称:“非营利组织”,数据:{relationship:},children:[{id:“0_10”,name:“Indigeni Digital”,data:{relationship:},children:[]},{id:“0_11”,name:“网站”,data:{relationship:”,Chiguire,data:{relationship:“},children:[]},data:{relationship:”,数据:{relation:“某些文本”}}}

然后我在一个图表引擎中使用这个var(json),我的想法是,如果我将_rhraph()返回的字符串复制到ajax上,并在javascript上对其进行硬编码,它会将字符串作为一个对象获取并绘制chard。但是如果让脚本从php脚本检索var,就像您可能认为这个var作为json=json.parse上的文本到达一样(result.rgraph);因为如果我对json发出警报,我看到的是内容而不是对象。因此,此时我将代码更改为:
json=json.parse(result.rgraph);


但这给了我一个错误:Uncaught SyntaxError:uncontracted token i。我不知道发生了什么,似乎json var的格式不正确,但为什么我硬编码var时它会工作?有什么想法吗?

您需要引用有效json的密钥,这不是有效的json。必须引用密钥名称,例如:

{ "id" : ...

只需将所有JSON键放在引号中(“id”、“etc”)…

我认为您必须使用
jQuery.parseJSON()
解析它:

json = jQuery.parseJSON(result);