duanhuanyou6478 2014-08-05 08:33
浏览 730
已采纳

修复格式错误的JSON

I have JSON bad formatted JSON file, I want to fix this with PHP code.

here is my JSON:

x: 322.5, y: 174, colors: #000000, removable: true, draggable: true, rotatable: true, resizable: true, scale: 1, degree: 0, price: 0, boundingBox: false, textSize: 22, source: , originX: 322.5, originY: 174, currentColor: #000000, text: დედა, font: Arial

Fixed output:

"x": 322.5, "y": 174, "colors": "#000000", "removable": true, "draggable": true, "rotatable": true, "resizable": true, "scale": 1, "degree": 0, "price": 0, "boundingBox": false, "textSize": 22, "source": , "originX": 322.5, "originY": 174, "currentColor": "#000000", "text": "დედა", "font": "Arial"

How to correct my JSON? PHP can handle it?

  • 写回答

2条回答 默认 最新

  • doujie7497 2014-08-05 08:43
    关注

    Although this is very hacky, but what if you do something like this?

    $str = 'x: 322.5, y: 174, colors: #000000, removable: true, draggable: true, rotatable: true, resizable: true, scale: 1, degree: 0, price: 0, boundingBox: false, textSize: 22, source: , originX: 322.5, originY: 174, currentColor: #000000, text: დედა, font: Arial';
    $arr = explode(',',$str);
    $tempArr = array();
    foreach($arr as $value){
        $temp = explode(':',$value);
        foreach($temp as $k=>$v){
            $v = trim($v);
            if($v === true || $v === "true")
                $temp[$k] = true;   
            elseif($v === false || $v === "false")
                $temp[$k] = false;
            else
                $temp[$k] = '"'.$v.'"'; 
        }
        $tempArr[] = implode(':', $temp );
    }
    echo implode(',', $tempArr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog