ray ray - 6 months ago 44
JSON Question

Fast JSON Parser for Matlab

Do you know a very fast JSON Parser for Matlab?

Currently I'm using JSONlab, but with larger JSON files (mine is 12 MB, 500 000 lines) it's really slow. Or do you have any tips' for me to increase the speed?

P.S. The JSON file is max. 3 levels deep.


If you want to be fast, you could use the Java JSON parser. And before this answer gets out of hand, I am going to post the stuff I put down so far:


% input example
jsonStr = '{"bool1": true, "string1": "some text", "double1": 5, "array1": [1,2,3], "nested": {"val1": 1, "val2": "one"}}'

% use java..
jsonObj = org.json.JSONObject(jsonStr);

% check out the available methods
jsonObj.methods % see also http://www.json.org/javadoc/org/json/JSONObject.html

% get some stuff
b = jsonObj.getBoolean('bool1')
s = jsonObj.getString('string1')
d = jsonObj.getDouble('double1')
i = jsonObj.getJSONObject('nested').getInt('val1')

% put some stuff
jsonObj = jsonObj.put('sum', 1+1);

% getting an array or matrix is not so easy (you get a JSONArray)
e = jsonObj.get('array1');

% what are the methods to access that JSONArray?

for idx = 1:e.length()

% but putting arrays or matrices works fine
jsonObj = jsonObj.put('matrix1', ones(5));

% you can get these also easily ..
m1 = jsonObj.get('matrix1')
% .. as long as you dont convert the obj back to a string
jsonObj = org.json.JSONObject(jsonObj.toString());
m2 = jsonObj.get('matrix1')