我有动态生成的php多维数组
Array
(
[0] => Array
(
[cid] => 73
[type] => 2
[qrystr] => Qtest1
[trck_no] => (570) 244-3738
[trgt_no] => 1919674683063
[webpage] => Array
(
[0] => Array
(
[wid] => 40
[page_url] => www.ctest2.com
)
[1] => Array
(
[wid] => 41
[page_url] => www.ctest3.com
)
)
)
[1] => Array
(
[cid] => 75
[type] => 3
[qrystr] => Qtest6
[trck_no] =>
[trgt_no] =>
[webpage] => Array
(
[0] => Array
(
[wid] => 42
[page_url] => www.test1.com
)
[1] => Array
(
[wid] => 43
[page_url] => www.test1.com
)
)
)
)
我想用Javascript转换它。为此,我已经使用json_encode()
php 对其进行了编码,这是相同的输出:
[{"cid":"73","type":"2","qrystr":"Qtest1","trck_no":"(570) 244-3738","trgt_no":"1919674683063","webpage":[{"wid":"40","page_url":"www.ctest2.com"},{"wid":"41","page_url":"www.ctest3.com"}]},{"cid":"75","type":"3","qrystr":"Qtest6","trck_no":"","trgt_no":"","webpage":[{"wid":"42","page_url":"www.test1.com"},{"wid":"43","page_url":"www.test1.com"}]}]
在javascript中,每当我使用它解析它时,JSON.parse()
我都会收到一条错误消息(即SyntaxError:JSON.parse:意外字符)。
请建议。
编辑:
我的 php 文件位于单独的服务器中,我以编程方式从基于“id”的数据库中收集信息并将其存储在一个数组中。
这是我的php代码:
foreach($campaigns as $campaign){
if(($campaign->type == 2) || ($campaign->type == 3)){
foreach($campaign->webpage as $webpage){
$webpages[] = array(
'wid'=>trim($webpage->id),
'page_url'=>trim($webpage->page_url)
);
}
}else{
$webpages = "";
}
$campaign_details[] = array(
'cid'=>trim($campaign->id),
'type'=>trim($campaign->type),
'qrystr'=>trim($campaign->qrystr),
'trck_no'=>trim($campaign->trcknmb->frnd_name),
'trgt_no'=>trim($campaign->trcknmb->trgtnmb->phone_no),
'webpage'=>$webpages
);
unset($webpages);
}
$cdetails = json_encode($campaign_details);
echo $cdetails;
我发出跨域请求以获取数组值。
这是我解析 json 字符串的 Javascript 代码:
function handler(evtXHR)
{
if (invocation.readyState == 4)
{
if (invocation.status == 200)
{
response = invocation.responseText;
var content = JSON.parse(response);
alert(content);
}
else
{
alert("Invocation Errors Occured");
}
}
}
我在这里放了一个警报功能来检查输出结果。