File size: 1,989 Bytes
796ff25 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
function mat=json2mat(filename)
% JSON2MAT Reads a JSON file
%
% mat=json2mat(filename)
%
% Input:
% filename: JSON filename (.json extension)
%
% Output:
% mat: Matlab cell array whose entries are Matlab structures containing the
% value for each JSON field
%
% Note: all numeric fields are rounded to double precision. Digits beyond
% double precision are lost.
%
% If you use this software in a publication, please cite:
%
% Jon Barker, Ricard Marxer, Emmanuel Vincent, and Shinji Watanabe, The
% third 'CHiME' Speech Separation and Recognition Challenge: Dataset,
% task and baselines, submitted to IEEE 2015 Automatic Speech Recognition
% and Understanding Workshop (ASRU), 2015.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright 2015 University of Sheffield (Jon Barker, Ricard Marxer)
% Inria (Emmanuel Vincent)
% Mitsubishi Electric Research Labs (Shinji Watanabe)
% This software is distributed under the terms of the GNU Public License
% version 3 (http://www.gnu.org/licenses/gpl.txt)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid=fopen(filename,'r');
fgetl(fid); % [
txt=fgetl(fid); % { or ]
txt=fgetl(fid); % first field
mat=cell(1);
ind=1; % entry index
while txt~=-1, % end of file
if strcmp(txt,' }, ') || strcmp(txt,' }'), % next entry
ind=ind+1;
txt=fgetl(fid); % { or ]
else
try
pos=strfind(txt,'"');
field=txt(pos(1)+1:pos(2)-1);
catch
keyboard;
end
if ~strcmp(txt(end-1:end),', '), % last field
txt=txt(pos(2)+3:end);
else
txt=txt(pos(2)+3:end-2);
end
if strcmp(txt(1),'"') && strcmp(txt(end),'"'), % text value
value=txt(2:end-1);
else % boolean or numerical value
value=eval(txt);
end
mat{ind}.(field)=value;
end
txt=fgetl(fid); % next field
end
fclose(fid);
return |