用于Matlab的快速JSON解析器 [英] Fast JSON Parser for Matlab

查看:583
本文介绍了用于Matlab的快速JSON解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Do you know a very fast JSON Parser for Matlab?

当前,我正在使用 JSONlab ,但是使用较大的JSON文件(我的文件大小为12 MB,500000行),速度确实很慢.还是您对我有任何提高速度的提示?

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. JSON文件最大. 3级深.

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

推荐答案

如果想提高速度,可以使用 Java JSON解析器. 在这个答案失控之前,我将发布到目前为止已经发表的内容:

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

这篇关于用于Matlab的快速JSON解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆