This function parses JSON strings. It converts JSON arrays into cell arrays and JSON objects into structures.
This can be used with webervices that return JSON data such as the API provided by Google®.
An example of use is:
google_search = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=matlab';
matlab_results = parse_json(urlread(google_search));
disp(matlab_results{1}.responseData.results{1}.titleNoFormatting)
disp(matlab_results{1}.responseData.results{1}.visibleUrl)
Works well, despite being a bit slow. Maybe it could be sped up with careful application of "regular expressions." Also, when you use the script, make sure the JSON string is a row vector of chars. Error may occur if string is a column vector of chars.
Thanks Joel, I thought maybe there would be an easy workaround you could point me to. But the new file solves the issue, it is faster by some magnitude. In any case, thanks for your work.
I know that the code is slow, and I made no attempt to make the code fast, just correct. François's version does seem to be faster, so if you need speed (and have a lot of strings), use his.
An alternative parser with faster handling of strings is available at http://www.mathworks.com/matlabcentral/fileexchange/23393
(another JSON Parser). For example, running the example above