此数据的格式是什么?是自定义格式吗?

what is the format of this data? is it a custom format?

本文关键字:格式 自定义 是什么 数据      更新时间:2023-09-26

我得到这个数据作为ajax响应:

{
    "idArray" = (
        "99516",
        "99518",
        "97344",
        "97345",
        "98425"
    );
    "frame" = {
        "size" = {
            "width" = "8";
            "height" = "8";
        };
        "origin" = {
            "x" = "244";
            "y" = "345";
        };
    };
},

这只是数据的一部分,但它以相同的格式继续。我无权访问生成此数据的文件的来源。

这是已知格式还是自定义格式?

由于人们倾向于在所有东西上抛出正则表达式,即使是不能用正则表达式解析的东西(即非正则语言): 我已经为这种数据格式编写了一个概念验证解析器:

$input = '{
    "idArray" = (
        "99516",
        "99518",
        "97344",
        "97345",
        "98425"
    );
    "frame" = {
        "size" = {
            "width" = "8";
            "height" = "8";
        };
        "origin" = {
            "x" = "244";
            "y" = "345";
        };
    };
}';
echo json_encode(parse($input));
function parse($input) {
    $tokens = tokenize($input);
    $index = 0;
    $result = parse_value($tokens, $index);
    if ($result[1] !== count($tokens)) {
        throw new Exception("parsing stopped at token " . $result[1] . " but there is more input");
    }
    return $result[0][1];
}
function tokenize($input) {
    $tokens = array();
    $length = strlen($input);
    $pos = 0;
    while($pos < $length) {
        list($token, $pos) = find_token($input, $pos);
        $tokens[] = $token;
    }
    return $tokens;
}
function find_token($input, $pos) {
    $static_tokens = array("=", "{", "}", "(", ")", ";", ",");
    while(preg_match("/'s/mis", substr($input, $pos, 1))) { // eat whitespace
        $pos += 1;
    }
    foreach ($static_tokens as $static_token) {
        if (substr($input, $pos, strlen($static_token)) === $static_token) {
            return array($static_token, $pos + strlen($static_token));
        }
    }
    if (substr($input, $pos, 1) === '"') {
        $length = strlen($input);
        $token_length = 1;
        while ($pos + $token_length < $length) {
            if (substr($input, $pos + $token_length, 1) === '"') {
                return array(array("value", substr($input, $pos + 1, $token_length - 1)), $pos + $token_length + 1);
            }
            $token_length += 1;
        }
    }
    throw new Exception("invalid input at " . $pos . ": `" . substr($input, $pos - 10, 20) . "`");
}
// value is either an object {}, an array (), or a literal ""
function parse_value($tokens, $index) {
    if ($tokens[$index] === "{") {  // object: a list of key-value pairs, glued together by ";"
        $return_value = array();
        $index += 1;
        while ($tokens[$index] !== "}") {
            list($key, $value, $index) = parse_key_value($tokens, $index);
            $return_value[$key] = $value[1];
            if ($tokens[$index] !== ";") {
                throw new Exception("Unexpected: " . print_r($tokens[$index], true));
            }
            $index += 1;
        }
        return array(array("object", $return_value), $index + 1);
    }
    if ($tokens[$index] === "(") {  // array: a list of values, glued together by ",", the last "," is optional
        $return_value = array();
        $index += 1;
        while ($tokens[$index] !== ")") {
            list($value, $index) = parse_value($tokens, $index);
            $return_value[] = $value[1];
            if ($tokens[$index] === ",") {  // last, is optional
                $index += 1;
            } else {
                if ($tokens[$index] !== ")") {
                    throw new Exception("Unexpected: " . print_r($tokens[$index], true));
                }
                return array(array("array", $return_value), $index + 1);
            }
        }
        return array(array("array", $return_value), $index + 1);
    }
    if ($tokens[$index][0] === "value") {
        return array(array("string", $tokens[$index][1]), $index + 1);
    }
    throw new Exception("Unexpected: " . print_r($tokens[$index], true));
}
// find a key (string) followed by '=' followed by a value (any value)
function parse_key_value($tokens, $index) {
    list($key, $index) = parse_value($tokens, $index);
    if ($key[0] !== "string") { // key must be a string
        throw new Exception("Unexpected: " . print_r($key, true));
    }
    if ($tokens[$index] !== "=" ) {
        throw new Exception("'=' expected");
    }
    $index += 1;
    list($value, $index) = parse_value($tokens, $index);
    return array($key[1], $value, $index);
}

输出为:

{"idArray":["99516","99518","97344","97345","98425"],"frame":{"size":{"width":"8","height":"8"},"origin":{"x":"244","y":"345"}}}

笔记

  • 原始输入具有尾随,。我已经删除了那个字符。如果您将其放回原处,它将抛出错误(更多输入)。

  • 这个解析器是幼稚的,因为它在开始解析之前标记了所有输入。这对于大投入不利。

  • 我没有在分词器中添加字符串的转义检测。比如:"foo'"bar"

这是一个有趣的练习。如果您有任何问题,请告诉我。

编辑:我看到这是一个JavaScript问题。将PHP移植到JavaScript不应该太难。list($foo, $bar) = func()相当于:var res = func(); var foo = res[0]; var bar = res[1];

尝试将此函数与响应文本一起使用作为参数:

function getJsonData(str){
    str = str.replace(/,/g, '')         //remove ,
             .replace(/'(/g, '[')       //replace (
             .replace(/'[/g)', ']')     //replace )
             .replace(/;/g, ',')        //replace ; 
             .replace(/=/g, ':');       //replace :
    return JSON.parse(str);
}

这是@SamSal所做的编辑

function getJsonData(str){
    str = str.replace(/'(/g, '[')       //replace (
         .replace(/')/g, ']')       //replace )
         .replace(/;'n's+}/g, '}')  //replace ;} with }
         .replace(/;/g, ',')        //replace remaining ; with ,  
         .replace(/=/g, ':');       //replace :
    return JSON.parse(str);
}

这是已知格式还是自定义格式?

它是一种自定义格式,看起来有点像 JSON,但实际上不是 JSON。