-2

I have php multidimensional array which is generated dynamically

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

)

and I want to convert it in Javascript. For that I have already encoded it using json_encode() in php and here is the output for the same :

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

In javascript whenever I parse this using JSON.parse() I get an error message (i.e. SyntaxError: JSON.parse: unexpected character).

Kindly suggest.

EDITED:

My php file is in separate server and where pogramatically I gather information from database based on "id" and store it in an array.

Here is my php code:

           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;

I make a cross domain request to get the array valu.

Here is my Javascript code to parse the json string:

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

I put an alert function here to check the output result.

3 Answers3

3

In Javascript, you don't need to parse it because it's a valid Javascript array as it is.

Simply:

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

Most likely you're getting the error because there's a problem with quoting the JSON when you call JSON.parse.

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • true, but might not be a bad idea to parse it in JS anyway. See this: http://stackoverflow.com/questions/6765106/is-json-parse-really-safer-than-eval-when-web-page-and-ajax-call-come-from-s – David Jul 09 '13 at 06:42
  • 1
    @David Array literal !== `eval`. If the value is properly JSON encoded, just treating it as a literal is fine. – deceze Jul 09 '13 at 06:48
  • @David there is no benefit to parsing it, and as deceze points out there are no security concerns with passing it this way. – MrCode Jul 09 '13 at 06:50
  • @deceze, I guess you're saying since all of these values are quoted. Supposing it's not all from one source but built the way the example on the link I sent is, then it's probably best no? – David Jul 09 '13 at 06:51
  • 2
    @David If you run it through `json_encode`, it's guaranteed to only consist of harmless strings and numbers. There's no chance to inject anything. If you're not doing that properly, then all bets are off anyway and anyone could inject anything even into the literal itself, against which parsing doesn't protect. – deceze Jul 09 '13 at 06:56
  • true :) I've always really seen people do JSON.parse for this kind of thing though, character encodings? I'm still not satisfied with not verifying it client side. Although it's not the kind of thing you really code exception cases for too often :) – David Jul 09 '13 at 07:00
0

You're not quoting the JSON as a string are you? Otherwise, that same json worked when I did a

JSON.parse('<your stuff>');
David
  • 2,715
  • 2
  • 22
  • 31
0

Did you replace " to \"? Because I have no problem executing:

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\"}]}]")
Paul Chen
  • 1,873
  • 1
  • 16
  • 28