gpt4 book ai didi

json - 用于 Matlab 的快速 JSON 解析器

转载 作者:行者123 更新时间:2023-12-02 01:02:49 25 4
gpt4 key购买 nike

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

目前我正在使用 JSONlab ,但是对于更大的 JSON 文件(我的是 12 MB,500 000 行)它真的很慢。或者你对我提高速度有什么建议吗?

附言JSON 文件最大。 3 层深。

最佳答案

如果你想快点,你可以使用 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')

关于json - 用于 Matlab 的快速 JSON 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com