2014-10-17 106 views
2

您是否知道用于Matlab的非常快速的JSON解析器?用于Matlab的快速JSON解析器

目前我使用的是JSONlab,但更大的JSON文件(我的是12 MB,500 000行)非常慢。或者你有什么提示可以提高速度?

P.S. JSON文件是最大的。 3层深。

回答

2

如果您想快点,可以使用Java JSON parser。 而这个答案失控之前,我要发布的东西,我放下至今:

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') 
+0

忘了提及您必须将'json.jar'添加到javaclasspath中。可以下载实例[这里](http://mvnrepository.com/artifact/org.json/json/20140107)。更新了答案。 – MeMyselfAndI 2014-10-17 12:09:54

0

如果你能负担得起调用.NET代码,你可能想看看在这个轻量级男人(我是作者):

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

巧合的是,我的基准包括在12MB球场测试(“父亲数据”)正是(和深度也是一对夫妇的水平),这解析器解析在便宜的笔记本电脑上,在250毫秒以内进入POCO。

对于MATLAB + .NET代码的集成:

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

“HTH,

0

如果你只是想读取JSON文件,并有一个C++编译器11,就可以使用非常快的json_read mex功能。