Javascript 这些数据的格式是什么?是自定义格式吗?
我将此数据作为ajax响应获取:Javascript 这些数据的格式是什么?是自定义格式吗?,javascript,jquery,dataformat,Javascript,Jquery,Dataformat,我将此数据作为ajax响应获取: { "idArray" = ( "99516", "99518", "97344", "97345", "98425" ); "frame" = { "size" = { "width" = "8"; "height" = "8"; }; "origin" = {
{
"idArray" = (
"99516",
"99518",
"97344",
"97345",
"98425"
);
"frame" = {
"size" = {
"width" = "8";
"height" = "8";
};
"origin" = {
"x" = "244";
"y" = "345";
};
};
},
这只是数据的一部分,但仍以相同的格式继续。
我无法访问生成此数据的文件的源
这是已知格式还是自定义格式?尝试使用此函数,并将响应文本作为参数:
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);
}
由于人们倾向于将正则表达式扔向所有东西,甚至是无法用正则表达式解析的东西(即非正则语言):我为这种数据格式编写了一个概念验证解析器:
$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);
}
注释
- 原始输入有一个尾随的
。我已经删除了那个角色。如果你把它放回去,它会抛出一个错误(更多的输入),
- 这个解析器是幼稚的,因为它在开始解析之前标记了所有输入。这对于大的输入是不好的
- 我没有在标记器中为字符串添加转义检测。比如:
“foo\”bar“
列表($foo,$bar)=func()
相当于:var res=func();var foo=res[0];var bar=res[1];
这是一种已知格式还是自定义格式
这是一种自定义格式,看起来有点像JSON。@DLeh-它不是JSON。为什么你希望不知道数据格式的人添加的标记包含正确数据格式的标记?这是不正确的JSON DLeh…相等的sign@Mechkov-至少还有三个非JSON fe我也想知道这种数据格式的名称,除非它是任意的,在这种情况下,你应该为他们没有使用JSON或类似的东西而感到不安。在我看来,这就像有人试图创建JSON对象,但不知道他或她在做什么。这是非常危险的。如果你的数据包含任何这些字符您将得到数据损坏。此数据格式似乎可以解析。是的,但在本例中,所有字段中只有数字,没有文本
{"idArray":["99516","99518","97344","97345","98425"],"frame":{"size":{"width":"8","height":"8"},"origin":{"x":"244","y":"345"}}}