php转类json格式的数据(没有双引号的JSON格式,一般在硬件对接中出现)

代码如下

 

= strlen($str)) {
                return "unexpected end when parsing object key";
            }
            $key = substr($str, $pos, $end - $pos);
            $pos = self::skip_ws($str, $end + 1);
            if ($pos >= strlen($str)) {
                return "unexpected end when parsing object key";
            }
            if ($str[$pos] == '[') {
                $res = self::parse_array($str, $pos);
                $value = $res['arr'];
                $pos = $res['pos'];
            } elseif ($str[$pos] == '{') {
                $res = self::parse_obj($str, $pos);
                $value = $res['obj'];
                $pos = $res['pos'];
            } else {
                $end = self::parse_str($str, $pos, True, [',', '}']);
                if ($end >= strlen($str)) {
                    return "unexpected end when parsing object key";
                }
                $value = substr($str, $pos, $end - $pos);
                $pos = $end;
            }
            $obj[$key] = $value;
            $pos = self::skip_ws($str, $pos);
            if ($pos >= strlen($str)) {
                return "unexpected end when parsing object key";
            }
            if ($str[$pos] == "}") {
                return ['obj' => $obj, 'pos' => $pos + 1];
            }
        }
    }

    private static function parse_array($str, $pos){
        $arr = [];
        while (True) {
            $pos = self::skip_ws($str, $pos + 1);
            if ($pos >= strlen($str)) {
                return "unexpected end when parsing object key";
            }
            if ($pos >= strlen($str)) {
                return "unexpected end when parsing object key";
            }
            if ($str[$pos] == "[") {
                $res = self::parse_array($str, $pos);
                $value = $res['arr'];
                $pos = $res['pos'];
            } elseif ($str[$pos] == "{") {
                $res = self::parse_obj($str, $pos);
                $value = $res['obj'];
                $pos = $res['pos'];
            } else {
                $end = self::parse_str($str, $pos, True, [',', ']']);
                if ($end >= strlen($str)) {
                    return "unexpected end when parsing object key";
                }
                $value = substr($str, $pos, $end - $pos);
                $pos = $end;
            }
            array_push($arr, $value);
            $pos = self::skip_ws($str, $pos);
            if ($pos >= strlen($str)) {
                return "unexpected end when parsing object key";
            }
            if ($str[$pos] == "]") {
                return ['arr' => $arr, 'pos' => $pos + 1];
            }
        }
    }
    #
    public static function parse($str) {
        if(!self::startsWith($str,"json")){
            return ["error_code"=>1,"msg"=>"请以【json:】开头"];
        };
        $pos = strpos($str, ':');
        if(empty($pos)){
            return false;
        }
        $pos = self::skip_ws($str, $pos+1);
        if($str[$pos] == '{'){
            return self::parse_obj($str,$pos)['obj'];
        }
        if($str[$pos] == '['){
            return self::parse_array($str,$pos)['arr'];
        }
    }
}

测试:

function test(){
    $str="json:{name:张三,age:22,book:[{bookname:语文,booksize:200,soce:100},{wkke:123}]}";
    $str = JsonParser::parse($str);
    echo json_encode($str);
}

你可能感兴趣的:(PHP)