gpt4 book ai didi

javascript - 这个数据的格式是什么?它是自定义格式吗?

转载 作者:数据小太阳 更新时间:2023-10-29 05:26:03 27 4
gpt4 key购买 nike

我将此数据作为 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"}}}

注释

  • 原始输入有尾随 ,。我已经删除了那个 Angular 色。如果你把它放回去,它会抛出一个错误(更多的输入)。

  • 从这个意义上说,这个解析器是天真的,它在开始解析之前标记所有输入。这对大输入不利。

  • 我没有在分词器中为字符串添加转义检测。喜欢:"foo\"bar"

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

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

关于javascript - 这个数据的格式是什么?它是自定义格式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27513468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com