-1

我有动态生成的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");
                }
              }
           }  

我在这里放了一个警报功能来检查输出结果。

4

3 回答 3

3

在 Javascript 中,您不需要解析它,因为它是一个有效的 Javascript 数组。

简单地:

var myData = <?php echo json_encode($data); ?>;

for(var i=0; i<myData.length; i++) {
    console.log( myData[i].cid );
    console.log( myData[i].type );
    console.log( myData[i].webpage[0].wid );
}

您很可能会收到错误,因为在调用 JSON.parse 时引用 JSON 存在问题。

于 2013-07-09T06:42:08.417 回答
0

您不是将 JSON 作为字符串引用吗?否则,当我执行相同的 json

JSON.parse('<your stuff>');
于 2013-07-09T06:41:52.283 回答
0

"换成\"?因为我执行没有问题:

JSON.parse("[{\"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\"}]}]")
于 2013-07-09T06:42:34.090 回答