doujin8673 2014-04-20 03:43
浏览 153

从json编码的数组中删除双引号

   $SQL = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'mst_sim_data' 
            AND COLUMN_NAME = 'status'";
   $result = mysql_query($SQL);
   $row = mysql_fetch_array($result);
   $enumList = explode(",", str_replace("'", '', substr($row['COLUMN_TYPE'], 5,     (strlen($row['COLUMN_TYPE'])-6))));

    foreach ($enumList as $key => $value) {
        $list["'$value'"] = $value;
    }       
    return json_encode($list);  

This returns following json string.

Object {'INSTOCK': "INSTOCK", 'ISSUED': "ISSUED", 'SOLDOUT': "SOLDOUT"}

But I need to replace single quotes over double quotes and it should be like this,

'INSTOCK': 'INSTOCK', 'ISSUED': 'ISSUED', 'SOLDOUT': 'SOLDOUT'}

How can I do this?

  • 写回答

2条回答 默认 最新

  • douruye5092 2014-04-20 03:50
    关注

    Is the goal to break JSON output? JSON strings and properties must use " as the quote.

    I believe the problem is two fold

    1. $list["'$value'"] = $value; should be $list["$value"] = $value; and

    2. the "s should be left alone


    If you do wish to mangle the quotes (which means the result is not JSON), then use the result of

    str_replace('"', "'", json_encode($list, JSON_HEX_APOS))
    

    Note the JSON_HEX_APOS flag, to prevent this gross hack from mutilating any JSON-string embedded ' characters.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?