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

2
  • Show more code what exactly you're doing. Commented Jul 9, 2013 at 6:39
  • Try running your output through jslint and see if there are any problems there? Commented Jul 9, 2013 at 7:28

3 Answers 3

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.

6
  • true, but might not be a bad idea to parse it in JS anyway. See this: stackoverflow.com/questions/6765106/… Commented Jul 9, 2013 at 6:42
  • 1
    @David Array literal !== eval. If the value is properly JSON encoded, just treating it as a literal is fine. Commented Jul 9, 2013 at 6:48
  • @David there is no benefit to parsing it, and as deceze points out there are no security concerns with passing it this way. Commented Jul 9, 2013 at 6: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? Commented Jul 9, 2013 at 6: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. Commented Jul 9, 2013 at 6:56
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>');
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\"}]}]")
1
  • 1
    but the value could have single quotes say {"a":"it's good"} Commented Jul 9, 2013 at 6:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.