Reading data from file with jsondecode
조회 수: 26 (최근 30일)
이전 댓글 표시
Hello everyone :)
I am struggling reading some data from a file using jsondecode function. I have the following data in a text file:
[0, [0,
3,
1,
['X3_5', 'X4_4', 'X4_5'],
[0, 0, 1],
[1,
[[1, [-41844328211318033252367, 344173910578922950492160]],
[0, [344173910578922950492160]],
[
[[0, [66782537082182499309963]],
4],
[[0, [-764979303944605824758909685951]],
19105490]
]]]]]:
Sometimes when I run
% path to text file
filePath = './myfile';
% Read the content of the file
fileID = fopen(filePath, 'r');
fileContent = fscanf(fileID, '%c');
fclose(fileID);
% Preprocess the content
fileContent = strrep(fileContent, '''', '"');
fileContent = strrep(fileContent, newline, '');
fileContent = strrep(fileContent, ':', '');
parsedStructure = jsondecode(fileContent);
I'm having two problems:
- A different value is stored, e.g., it saves -41844328211318034137088 instead -41844328211318033252367. How can I save the values in exact arithmetic?
- It ignores some brackets, e.g., the first list in [[0, [66782537082182499309963]],4] is merged as follows:
parsedStructure{2}{6}{2}{3}{1}{1}
0
6.6783e+22
parsedStructure{2}{6}{2}{3}{1}{2}
4
I have tried to solve the second problem using several nested if statements. However, I feel it is adding unnecesary complexity to the code and it just fails everytime it finds another similar situation. Any advice or suggestion is well received :)
댓글 수: 0
답변 (1개)
Taylor
2024년 2월 6일
Have you tried readstruct? It will create a structure from JSON data, and you can even parse by selected standards.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 JSON Format에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!