简体   繁体   中英

multiple JSON object response

I have a situation where i have a drop down in php file 'A'.and when i select a value from that drop down an ajax call is made and a different php file B is triggered. In that file,i do db fetch,and form two json objects.these two json objects are required for me to draw 2 different data tables and 2 different charts.

  1. when i echo one json object in 'B' i get that as a response to ajax call in 'A'

  2. I cant even get a response if i echo two json objects in B.

  3. for a single json object response,i can draw datatable and can manipulate json object with javascript and hopefully draw chart

Please advise how to handle this situation Code from file B

        $json = json_encode($tot_categ);
        $json_percent = json_encode($tot_que_percent);
            $cols_percent = array(
                array( 'id' => 't', 'label' => 'Title', 'type' => 'string'),
                array( 'id' => 'n', 'label' => 'Neutral(+) ', 'type' => 'string'),
                array('id' => 'a', 'label' => 'Agree', 'type' => 'string'),
                array('id' => 'ne', 'label' => 'Neutral(-)', 'type' => 'string'),
                array('id' => 'd', 'label' => 'Disagree', 'type' => 'string'),

        );

        $jcols_percent = json_encode($cols_percent);
        //JSON format accepted by Google tables
        $r_percent= "{cols:".$jcols_percent.','."rows:".$json_percent."}";
        //echo (JSON.stringify($r_percent));
        // echo $r_percent;

        $cols = array(
                array( 'id' => 't', 'label' => 'Title', 'type' => 'string'),
                array( 'id' => 'l', 'label' => 'Avg ', 'type' => 'string'),
                array('id' => 'lb', 'label' => 'High', 'type' => 'string'),
                array('id' => 'lo', 'label' => 'Low', 'type' => 'string')
        );

        $jcols = json_encode($cols);
        //JSON format accepted by Google tables
        $r = "{cols:".$jcols.','."rows:".$json."}";
        //echo(json_decode($r));
        echo $r;

$r and $r_percent are my objects

$r_percent when echoed gives

{cols:[{"id":"t","label":"Title","type":"string"},{"id":"n","label":"Neutral(+) ","type":"string"},{"id":"a","label":"Agree","type":"string"},{"id":"ne","label":"Neutral(-)","type":"string"},{"id":"d","label":"Disagree","type":"string"}],rows:[{"c":[{"v":"165q"},{"v":0},{"v":0},{"v":0},{"v":100}]},{"c":[{"v":"160q"},{"v":0},{"v":0},{"v":0},{"v":6}]},{"c":[{"v":"161q"},{"v":0},{"v":0},{"v":0},{"v":10}]},{"c":[{"v":"162q"},{"v":7},{"v":0},{"v":7},{"v":0}]},{"c":[{"v":"163q"},{"v":0},{"v":25},{"v":0},{"v":0}]},{"c":[{"v":"164q"},{"v":0},{"v":100},{"v":0},{"v":0}]}]}

$r when echoed gives

{cols:[{"id":"t","label":"Title","type":"string"},{"id":"l","label":"Avg ","type":"string"},{"id":"lb","label":"High","type":"string"},{"id":"lo","label":"Low","type":"string"}],rows:[{"c":[{"v":"165q"},{"v":1.3333333333333},{"v":"2"},{"v":"1"}]},{"c":[{"v":"160q"},{"v":6},{"v":"10"},{"v":"1"}]},{"c":[{"v":"161q"},{"v":6.6666666666667},{"v":"9"},{"v":"2"}]},{"c":[{"v":"162q"},{"v":7},{"v":"9"},{"v":"3"}]},{"c":[{"v":"163q"},{"v":8},{"v":"9"},{"v":"6"}]},{"c":[{"v":"164q"},{"v":5},{"v":"5"},{"v":"5"}]}]}

when combined

$result = array('objA' => $r_percent, 'objB' => $r );
        echo json_encode($result);

updated echoing

{"objA":"{cols:[{\"id\":\"t\",\"label\":\"Title\",\"type\":\"string\"},{\"id\":\"n\",\"label\":\"Neutral(+) \",\"type\":\"string\"},{\"id\":\"a\",\"label\":\"Agree\",\"type\":\"string\"},{\"id\":\"ne\",\"label\":\"Neutral(-)\",\"type\":\"string\"},{\"id\":\"d\",\"label\":\"Disagree\",\"type\":\"string\"}],rows:[{\"c\":[{\"v\":\"165q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":100}]},{\"c\":[{\"v\":\"160q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":6}]},{\"c\":[{\"v\":\"161q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":10}]},{\"c\":[{\"v\":\"162q\"},{\"v\":7},{\"v\":0},{\"v\":7},{\"v\":0}]},{\"c\":[{\"v\":\"163q\"},{\"v\":0},{\"v\":25},{\"v\":0},{\"v\":0}]},{\"c\":[{\"v\":\"164q\"},{\"v\":0},{\"v\":100},{\"v\":0},{\"v\":0}]}]}","objB":"{cols:[{\"id\":\"t\",\"label\":\"Title\",\"type\":\"string\"},{\"id\":\"l\",\"label\":\"Avg \",\"type\":\"string\"},{\"id\":\"lb\",\"label\":\"High\",\"type\":\"string\"},{\"id\":\"lo\",\"label\":\"Low\",\"type\":\"string\"}],rows:[{\"c\":[{\"v\":\"165q\"},{\"v\":1.3333333333333},{\"v\":\"2\"},{\"v\":\"1\"}]},{\"c\":[{\"v\":\"160q\"},{\"v\":6},{\"v\":\"10\"},{\"v\":\"1\"}]},{\"c\":[{\"v\":\"161q\"},{\"v\":6.6666666666667},{\"v\":\"9\"},{\"v\":\"2\"}]},{\"c\":[{\"v\":\"162q\"},{\"v\":7},{\"v\":\"9\"},{\"v\":\"3\"}]},{\"c\":[{\"v\":\"163q\"},{\"v\":8},{\"v\":\"9\"},{\"v\":\"6\"}]},{\"c\":[{\"v\":\"164q\"},{\"v\":5},{\"v\":\"5\"},{\"v\":\"5\"}]}]}"}

Combine both responses in a single object, return that object and use both parts separately again:

PHP side:

$result = array( 'objA' => $objA, 'objB' => $objB );
echo json_encode( $result );

Client side:

// your success callback handler
function handler( data ) {
  // execute code for object A
  doStuff( data.objA );
  // execute code for object B
  doOtherStuff( data.objB );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM