【问题标题】:Fast JSON Parser for MatlabMatlab 的快速 JSON 解析器
【发布时间】:2021-08-11 21:34:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: json matlab parsing


    【解决方案1】:

    如果你想更快,你可以使用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.jar 添加到javaclasspath。可以下载例如here。也更新了答案。
    【解决方案2】:

    如果你有能力调用 .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,

    【讨论】:

      【解决方案3】:

      如果您只想读取 JSON 文件,并且拥有 C++11 编译器,则可以使用非常快的 json_read mex 函数。

      【讨论】:

        【解决方案4】:

        从 Matlab 2016b 开始,您可以使用jsondecode

        我没有将它的性能与其他实现进行比较。 从个人经验来看,我可以说它并不是非常慢。

        【讨论】:

          猜你喜欢
          • 2014-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多