save json file to directory from workspace

I've read a json file into a structure, edited the structure, re-encoded the structure as another json file in the workspace, then attempted to save it in the directory using the save function. It appears to have an issue here as instead of saving correctly it seems to be encoding wrong as shown below. first image is the view when i open the original json file which is correctly displayed. second image is the view when i open the new json file from the directory. it is stored correctly in the workspace so the issue is amost certainly from the save function - what is the correct method for saving the json file?
Thanks,

 Respuesta aceptada

Jan
Jan el 24 de Mzo. de 2018
Editada: Jan el 24 de Mzo. de 2018

0 votos

No, this is not a "wrong" encoding, but save stores the data in MAT file format. sav is simply the wrong function to store a JSON file.
What did you use to import the JSON file?
I'd start with searching in the net. I'd prefer a stable Java implementation like http://vision.is.tohoku.ac.jp/~kyamagu/software/json/. You find many other implementations in the FileExchange: https://www.mathworks.com/matlabcentral/fileexchange/?utf8=%E2%9C%93&term=json . JSONlab is frequently downloaded, but I'm not a fan of it because of the usage of global variables for the communication with the subfunctions. What a pity that such a huge and useful tool suffers from a design flaw.
Of course Matlab's jsonencode should be fine.

4 comentarios

George Swindells
George Swindells el 24 de Mzo. de 2018
Editada: Walter Roberson el 24 de Mzo. de 2018
fname = '6f_F_6f.json';
fid = fopen(fname);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
data = jsondecode(str);
for k = 1:13
direc = data.raceTrack.sections(k).waypoints;
latvals = {direc.lat}';
lonvals = {direc.lon}';
arr = [cell2mat(latvals), cell2mat(lonvals)];
newlatvals = [];
newlonvals = [];
for i = 2:size(arr,1)
newlatvals(i-1,1) = (arr(i-1,1)+arr(i,1))/2;
newlonvals(i-1,1) = (arr(i-1,2)+arr(i,2))/2;
end
newarrlat = [];
newarrlon = [];
for i = 1:size(newlatvals,1)
newarrlat = [newarrlat ; arr(i,1) ; newlatvals(i,1)];
newarrlon = [newarrlon ; arr(i,2) ; newlonvals(i,1)];
end
newarrlat = [newarrlat ; arr(i+1,1)];
newarrlon = [newarrlon ; arr(i+1,2)];
for j = 1:size(newarrlat,1)
data.raceTrack.sections(k).waypoints(j) = struct('lat', {(newarrlat(j))}, 'lon', {(newarrlon(j))});
end
end
This is the script I'm running to import and alter the json file, then i run jsonencode function as seen in the command window which shows the correct looking output in 'ans', on which I then run save('6f_F_6f2.json', 'ans') expecting it to save the output as a json file under the name 6f_F_6f2.json,
I was hoping there was a matlab function installed as standard that I hadn't seen, thanks for the suggestions
I'm not sure if you understood my answer: save is the wrong command, because it creates MAT files. There is a ASCII output also, but this is not what you need. Better:
jsonStr = jsonencode(YourStruct);
fid = fopen('6f_F_6f2.json', 'w');
if fid == -1, error('Cannot create JSON file'); end
fwrite(fid, jsonStr, 'char');
fclose(fid);
Note: Using "ans" directly is a bad idea, because this variable is replaced automatically. This impedes debugging. Better catch the output of functions explicitly.
George Swindells
George Swindells el 24 de Mzo. de 2018
Yeah I understood that thanks, and thanks a lot for the example code there, Yeah I don’t normally use “ans” I was just using it to test the output, i will define something properly next.
Thanks again,
To import json file to matlab
DF = 'filename.json';
fid = fopen(DF);
raw = fread(fid,inf);
str = char(raw');
data = jsondecode(str);

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 24 de Mzo. de 2018

Comentada:

el 27 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by