how to solve extra text error when reading json files?

50 Ansichten (letzte 30 Tage)
Tomás
Tomás am 3 Aug. 2023
Kommentiert: Voss am 4 Aug. 2023
I am trying to read a json file from an arduino board in matlab with the following script:
% fileName = 'test1.json'; % filename in JSON extension
% str = fileread(fileName); % dedicated for reading files as text
% data = jsondecode(str); % Using the jsondecode function to parse JSON from string
but I get the following error:
Error using jsondecode JSON syntax error at line 1, column 79 (character 79): additional text. Error in read_json (line 4) data = jsondecode(str); % Using the jsondecode function to parse JSON from a string
I imagine it must be something with the quotes, commas or brackets...
I need to extract the acceleration values to use them in matlab later (acc_x, acc_y, and acc_z). The json fille is:
{"acc_x": 0.24, "acc_y": 0.78, "acc_z": 0.46, "Timestamp": 1691078254.4053023},
{"acc_x": 0.27, "acc_y": 0.78, "acc_z": 0.47, "Timestamp": 1691078254.4153075},
{"acc_x": 0.29, "acc_y": 0.78, "acc_z": 0.5, "Timestamp": 1691078254.43097},
{"gy_x": -35.89, "gy_y": 3.78, "gy_z": 14.4, "Timestamp": 1691078254.43097},
{"acc_x": 0.32, "acc_y": 0.78, "acc_z": 0.47, "Timestamp": 1691078254.4409742},
{"gy_x": -33.39, "gy_y": 2.62, "gy_z": 13.98, "Timestamp": 1691078254.4409742},
{"acc_x": 0.32, "acc_y": 0.78, "acc_z": 0.44, "Timestamp": 1691078254.4409742},
{"gy_x": -29.79, "gy_y": 1.83, "gy_z": 8.79, "Timestamp": 1691078254.4409742},
{"acc_x": 0.27, "acc_y": 0.8, "acc_z": 0.41, "Timestamp": 1691078254.456637},
{"gy_x": -25.57, "gy_y": -0.37, "gy_z": 7.51, "Timestamp": 1691078254.456637},
{"acc_x": 0.25, "acc_y": 0.82, "acc_z": 0.44, "Timestamp": 1691078254.4666882},
{"gy_x": -25.57, "gy_y": -1.65, "gy_z": 8.85, "Timestamp": 1691078254.4767776},
{"acc_x": 0.22, "acc_y": 0.82, "acc_z": 0.46, "Timestamp": 1691078254.4767776},
{"gy_x": -28.81, "gy_y": -0.31, "gy_z": 11.29, "Timestamp": 1691078254.4767776},
{"acc_x": 0.23, "acc_y": 0.82, "acc_z": 0.46, "Timestamp": 1691078254.486785},

Akzeptierte Antwort

Voss
Voss am 3 Aug. 2023
% I had to change the extension to upload the file, so here I change it back:
movefile('test1.txt','test1.json')
Try this:
txt = fileread('test1.json')
txt =
'{"acc_x": 0.24, "acc_y": 0.78, "acc_z": 0.46, "Timestamp": 1691078254.4053023}, {"acc_x": 0.27, "acc_y": 0.78, "acc_z": 0.47, "Timestamp": 1691078254.4153075}, {"acc_x": 0.29, "acc_y": 0.78, "acc_z": 0.5, "Timestamp": 1691078254.43097}, {"gy_x": -35.89, "gy_y": 3.78, "gy_z": 14.4, "Timestamp": 1691078254.43097}, {"acc_x": 0.32, "acc_y": 0.78, "acc_z": 0.47, "Timestamp": 1691078254.4409742}, {"gy_x": -33.39, "gy_y": 2.62, "gy_z": 13.98, "Timestamp": 1691078254.4409742}, {"acc_x": 0.32, "acc_y": 0.78, "acc_z": 0.44, "Timestamp": 1691078254.4409742}, {"gy_x": -29.79, "gy_y": 1.83, "gy_z": 8.79, "Timestamp": 1691078254.4409742}, {"acc_x": 0.27, "acc_y": 0.8, "acc_z": 0.41, "Timestamp": 1691078254.456637}, {"gy_x": -25.57, "gy_y": -0.37, "gy_z": 7.51, "Timestamp": 1691078254.456637}, {"acc_x": 0.25, "acc_y": 0.82, "acc_z": 0.44, "Timestamp": 1691078254.4666882}, {"gy_x": -25.57, "gy_y": -1.65, "gy_z": 8.85, "Timestamp": 1691078254.4767776}, {"acc_x": 0.22, "acc_y": 0.82, "acc_z": 0.46, "Timestamp": 1691078254.4767776}, {"gy_x": -28.81, "gy_y": -0.31, "gy_z": 11.29, "Timestamp": 1691078254.4767776}, {"acc_x": 0.23, "acc_y": 0.82, "acc_z": 0.46, "Timestamp": 1691078254.486785},'
txt = ['[' txt(1:end-1) ']']
txt =
'[{"acc_x": 0.24, "acc_y": 0.78, "acc_z": 0.46, "Timestamp": 1691078254.4053023}, {"acc_x": 0.27, "acc_y": 0.78, "acc_z": 0.47, "Timestamp": 1691078254.4153075}, {"acc_x": 0.29, "acc_y": 0.78, "acc_z": 0.5, "Timestamp": 1691078254.43097}, {"gy_x": -35.89, "gy_y": 3.78, "gy_z": 14.4, "Timestamp": 1691078254.43097}, {"acc_x": 0.32, "acc_y": 0.78, "acc_z": 0.47, "Timestamp": 1691078254.4409742}, {"gy_x": -33.39, "gy_y": 2.62, "gy_z": 13.98, "Timestamp": 1691078254.4409742}, {"acc_x": 0.32, "acc_y": 0.78, "acc_z": 0.44, "Timestamp": 1691078254.4409742}, {"gy_x": -29.79, "gy_y": 1.83, "gy_z": 8.79, "Timestamp": 1691078254.4409742}, {"acc_x": 0.27, "acc_y": 0.8, "acc_z": 0.41, "Timestamp": 1691078254.456637}, {"gy_x": -25.57, "gy_y": -0.37, "gy_z": 7.51, "Timestamp": 1691078254.456637}, {"acc_x": 0.25, "acc_y": 0.82, "acc_z": 0.44, "Timestamp": 1691078254.4666882}, {"gy_x": -25.57, "gy_y": -1.65, "gy_z": 8.85, "Timestamp": 1691078254.4767776}, {"acc_x": 0.22, "acc_y": 0.82, "acc_z": 0.46, "Timestamp": 1691078254.4767776}, {"gy_x": -28.81, "gy_y": -0.31, "gy_z": 11.29, "Timestamp": 1691078254.4767776}, {"acc_x": 0.23, "acc_y": 0.82, "acc_z": 0.46, "Timestamp": 1691078254.486785}]'
C = jsondecode(txt);
C{:} % check the contents of C
ans = struct with fields:
acc_x: 0.2400 acc_y: 0.7800 acc_z: 0.4600 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.2700 acc_y: 0.7800 acc_z: 0.4700 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.2900 acc_y: 0.7800 acc_z: 0.5000 Timestamp: 1.6911e+09
ans = struct with fields:
gy_x: -35.8900 gy_y: 3.7800 gy_z: 14.4000 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.3200 acc_y: 0.7800 acc_z: 0.4700 Timestamp: 1.6911e+09
ans = struct with fields:
gy_x: -33.3900 gy_y: 2.6200 gy_z: 13.9800 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.3200 acc_y: 0.7800 acc_z: 0.4400 Timestamp: 1.6911e+09
ans = struct with fields:
gy_x: -29.7900 gy_y: 1.8300 gy_z: 8.7900 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.2700 acc_y: 0.8000 acc_z: 0.4100 Timestamp: 1.6911e+09
ans = struct with fields:
gy_x: -25.5700 gy_y: -0.3700 gy_z: 7.5100 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.2500 acc_y: 0.8200 acc_z: 0.4400 Timestamp: 1.6911e+09
ans = struct with fields:
gy_x: -25.5700 gy_y: -1.6500 gy_z: 8.8500 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.2200 acc_y: 0.8200 acc_z: 0.4600 Timestamp: 1.6911e+09
ans = struct with fields:
gy_x: -28.8100 gy_y: -0.3100 gy_z: 11.2900 Timestamp: 1.6911e+09
ans = struct with fields:
acc_x: 0.2300 acc_y: 0.8200 acc_z: 0.4600 Timestamp: 1.6911e+09

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by