json格式化字符串到数字

时间:2012-06-19 15:42:36

标签: json visualization

我的Json输出生成;

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

等。等

google visualization api要求输出output9元素的数字格式,例如: "output9": 269000代替"output9": "269000"。我怎样才能为这个元素实现这个目标?

我的json.php生成如下的json输出:

 ?>
 {
 "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 个答案:

答案 0 :(得分:1)

这取决于您如何生成JSON。

例如,如果您使用的是Ruby后端,则可以调用:

"output9" => output9.to_i

有不同语言的各种帮助方法(例如Java和Javascript具有parseInt()函数)将字符串更改为整数。

修改

如果您的JSON是由PHP生成的,请将字符串转换为整数:

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

那应该摆脱引号。