0

My Json output generates;

[ 
  {
    "a1_id":"7847TK10", 
    "output2":"7847TK10", 
    "output4":"something", 
    "output5":"3stars.gif", 
    "output9": "269000", 
...

etc. etc.

The google visualization api asks for a number format for the output9 element e.g.: "output9": 269000 instead of "output9": "269000". How can I achieve this for this element?

My json.php generates the json output like this:

 ?>
 {
 "total": <?php echo $total ?>,
 "success": true,
 "rows": [

    // Iterate over the rows
    $nextRow= $result->nextRow();
    $r      = 1;
    $info   = array();

    while ( $nextRow ) {


        $nextColumn = $result->nextColumn();

        // Has this column been printed already
        if ( $unique )
        {
            $d = $result->getDataForField($unique);
            if ( array_key_exists($d, $already) )
            {
                $nextRow= $result->nextRow();
                continue;
            }
            $already[$d] = true;
        }

        echo '{';
        // Iterate over the columns in each row

        while ( $nextColumn )
        {

            // Get the variable
            $variable       = $result->getOutputVariable();
            $name           = $variable->getName(true);
            $data           = $result->getDataForField();

            if ( !isset($info[$name]) ) {
                $info[$name]['translate']   = $variable->shouldTranslate();
                $info[$name]['type']        = $variable->getDataType();
                $info[$name]['linkable']    = $variable->isLinkable();
            }

            // Translate the data if requested
            if ( $info[$name]['translate'] ) {
                $data   = LQMTemplate::_($data);
            }

            $data   = $variable->format($data, false);

            $type   = $info[$name]['type'];
            if ( ($type == 'bool') or ($type == 'boolean') )
            {
                $data = $data ? '1' : '0';
                echo "'$name':$data";
            } elseif ( $encode ) {
                // Can we use json_encode ?
                // str_replace because some versions of PHP have a bug that will over escape forward slashes
                echo "\"$name\":".str_replace('\\/', '/', json_encode($data));
            } else {
                $data   = LQMUtility::jsonEscape($data, '"');
                //echo "'$name':\"$data\"";
                echo "\"$name\":\"$data\"";



            }

            // Conditionally print the next column
            $nextColumn = $result->nextColumn();
            if ( $nextColumn ) echo ",\n ";

        }


        // Conditionally print the next column
        $nextRow = $result->nextRow();

        echo $nextRow ? "},\n" : "}\n";
        $r++;

    }

unset($result);
echo ']}';
}
}

1 Answers1

1

This depends on how you are generating your JSON.

For example, if you were using a Ruby backend, you could call:

"output9" => output9.to_i

There are various helper methods in different languages (e.g. Java and Javascript have parseInt() functions) to change a string into an integer.

Edit:

If your JSON is being generated by PHP, cast the string to an integer:

$json['output9'] = int($output9_value);

That should get rid of the quotation marks.

parkr
  • 823
  • 6
  • 14
  • I am calling the json with jquery – Peter van der Lely Jun 19 '12 at 19:33
  • I've added my json.php code which generates my JSON in my question. – Peter van der Lely Jun 19 '12 at 19:40
  • I am calling the json like this: $.getJSON('regiontop11.json',etc.etc. $.each(data, function(i,v) { // set the values for both the name and the population map.setValue(i, 0, v.output6); map.setValue(i, 1, v.output9); – Peter van der Lely Jun 19 '12 at 19:42
  • this helped me finding a solution (using the converter function from jquery):http://stackoverflow.com/questions/5834901/jquery-automatic-type-conversion-during-parse-json – Peter van der Lely Jun 19 '12 at 21:45
  • Ha Parkr, as you could see from my json.php code: echo "\"$name\":\"$data\""; everything (strings or numbers is within double quotes. Its valid json also for numeric data. Only problem is that's not valid json for google visualisation api. How can I implement your suggestion $json['output9'] = int($output9_value) in my code (see code above)? – Peter van der Lely Jun 20 '12 at 09:42
  • added an elsif in my php: } elseif ($name == 'output9') { echo "\"$name\":$data"; so no more echo "\"$name\":\"$data\""; and double quotes for 'output9' – Peter van der Lely Jun 20 '12 at 12:51
  • @ parkr ; it does! some api's don't mind about this, but google sure handles json as a purist! – Peter van der Lely Jun 21 '12 at 21:24