S
S
Serge Tkach2017-09-22 18:04:28
JSON
Serge Tkach, 2017-09-22 18:04:28

What is wrong with encoding when parsing json from 1C to php?

There are the following lines:

//$json = '[{"sku":"5200001200057","model":"516187A"},{"sku":"5200001200002","model":"521387"}]'; // work typed manually as prospekt.json 
$json   = '[{"sku":"5200001200057","model":"516187A"},{"sku":"5200001200002","model":"521387"}]'; // prospekt.json not work 

$res = json_decode($json, true); // возвращает NULL

I check the error:
switch (json_last_error()) {
  case JSON_ERROR_NONE:
    echo ' - Ошибок нет';
    break;
  case JSON_ERROR_DEPTH:
    echo ' - Достигнута максимальная глубина стека';
    break;
  case JSON_ERROR_STATE_MISMATCH:
    echo ' - Некорректные разряды или не совпадение режимов';
    break;
  case JSON_ERROR_CTRL_CHAR:
    echo ' - Некорректный управляющий символ';
    break;
  case JSON_ERROR_SYNTAX:
    echo ' - Синтаксическая ошибка, не корректный JSON';
    break;
  case JSON_ERROR_UTF8:
    echo ' - Некорректные символы UTF-8, возможно неверная кодировка';
    break;
  default:
    echo ' - Неизвестная ошибка';
    break;
}

Gives "Syntax error, not valid JSON"
I generally thought that most likely there would be an error related to the encoding. Since the original file is decoded normally. And when re-saving, it is no longer decorated. But php says that I have a syntax error, how can I understand this?
--
The server response at the location of the required json file is missing
Content-type: application/json; charset=UTF-8
But the file itself is not generated at the time of access, therefore, the charset header is missing
--
In the ideone.com editor, when inserting json directly from the json file, I did not see any extra characters, as described in the comments. But I saw them using the FAR editor.
59cba72d7ef96849512851.pngSolving the problem
I sent a snapshot to 1C-nickname.
Answer:
"The first 3 characters are a UTF-8 marker. It is converted into an "artifact" because it is read bit by bit and not as a text document, therefore it reads this marker as well. Healed at the level of bitwise operations - removed the UTF-8 marker."

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Sanych, 2017-09-22
@SergeTkach

When pasted into the ideone editor, it turned out that there are some artifacts in your json-e))
Remove artifacts and voila https://ideone.com/YlDWAx

V
VetalM82, 2021-10-13
@VetalM82

On the php side
$result = trim($result, "\xEF\xBB\xBF");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question