Fast JSON Parser for Matlab

2019-05-20 02:30发布

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.

3条回答
太酷不给撩
2楼-- · 2019-05-20 03:08

If you can afford to call .NET code, you may want to have a look at this lightweight guy (I'm the author):

https://github.com/ysharplanguage/FastJsonParser#PerfDetailed

Coincidentally, my benchmark includes a test ("fathers data") in the 12MB ballpark precisely (and with a couple levels of depth also) that this parser parses into POCOs in under 250 ms on my cheap laptop.

As for the MATLAB + .NET code integration:

http://www.mathworks.com/help/matlab/using-net-libraries-in-matlab.html

'HTH,

查看更多
Lonely孤独者°
3楼-- · 2019-05-20 03:10

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:

clc

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

% use java..
javaaddpath('json.jar');
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?
e.methods

for idx = 1:e.length()
    e.get(idx-1)
end

% 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')
查看更多
萌系小妹纸
4楼-- · 2019-05-20 03:18

If you just want to read JSON files, and have a C++11 compiler, you can use the very fast json_read mex function.

查看更多
登录 后发表回答