2014-12-16 10 views
6

uzyskać te dane jako ajax odpowiedzi:jaki jest format tych danych? czy to jest niestandardowy format?

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

To jest tylko część danych, ale nadal w tym samym formacie. Nie mam dostępu do źródła plików, które generują te dane.

Jest to znany formacie lub coś niestandardowego?

+0

@DLeh - To nie jest JSON. Dlaczego spodziewałbyś się, że tagi dodane przez kogoś, kto nie wie, jaki format danych jest dołączony do tagu dla właściwego formatu danych? – Quentin

+0

To niewłaściwe JSON DLeh ... znaku równości – Mechkov

+0

@Mechkov - Istnieją co najmniej trzy inne funkcje niż w formacie JSON danych. – Quentin

Odpowiedz

3

Ponieważ ludzie wydają się rzucać wyrażeń regularnych na wszystko, nawet rzeczy których nie można przeanalizować za pomocą wyrażeń regularnych (tj. w językach nieregularnych): Napisałem parser typu proof-of-concept dla tego formatu danych:

$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); 
} 

Wyjście jest:

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

Uwagi

  • oryginalny wejściowy ma spływu ,. Usunąłem tę postać. Spowoduje to zgłoszenie błędu (więcej danych wejściowych), jeśli go odłożysz.

  • Ten parser jest naiwny w tym sensie, że tokenizes wszystkie dane wejściowe przed rozpoczęciem przetwarzania. To nie jest dobre dla dużych wejść.

  • Nie dodaliśmy wykrywanie ucieczki ciągów w tokenizera. Podoba się: "foo\"bar".

To było zabawne ćwiczenie. Jeśli masz jakieś pytania, daj mi znać.

Edycja: Widzę, że jest to kwestia JavaScript. Przeniesienie PHP na JavaScript nie powinno być zbyt trudne. Numer list($foo, $bar) = func() jest równoważny z: var res = func(); var foo = res[0]; var bar = res[1];

1

Spróbuj użyć tej funkcji z tekstem odpowiedzi jako parametr:

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

Jest to edycja wykonana przez @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); 
} 
+3

To jest bardzo niebezpieczne. Jeśli twoje dane zawierają którąkolwiek z tych postaci, otrzymasz uszkodzenie danych. Ten format danych wydaje się parsowalny. – Halcyon

+0

Tak, ale w tym przypadku są tylko liczby we wszystkich polach, nie ma tekstu – Ragnar

0

Czy to znany format czy coś niestandardowego?

Jest to format niestandardowy, który wygląda trochę trochę jak JSON bez rzeczywiście JSON.

Powiązane problemy