如何在 Octave 中解析 json 文件?

How to parse json file in Octave?

如题,Octave中有parsing/decoding一个.json文件的内置方式吗?

我浏览了 Octave 文档和不同的工具,但我唯一找到的是这个工具: https://github.com/fangq/jsonlab

编辑: 目的是能够为两个不同的环境使用相同的 json 配置文件:python 和 Octave。所以会是: 1.定义配置; 2. 运行 octave脚本,从config.json读取配置; 3. 运行 python 脚本,从 config.json;

读取配置

我目前正在使用 json八度实验室工具集,并且由于 json 相当简单,所以它工作得非常好。问题纯粹出于好奇,为什么 Octave 默认不实现 json 序列化库。

所以,由于 json 很简单,我的解决方案是使用 https://github.com/fangq/jsonlab。从下面的评论来看,似乎不能完美地用于更复杂的 jsons.

我已经在很多项目中使用 JSONlab 很长时间了,但是因为它很慢而且有些点没有达到我的期望我围绕 rapidjson 写了一个八度包装器:https://github.com/Andy1978/octave-rapidjson

README.md 显示了一些示例。一个从服务器获取回复的示例 JSON,然后将其转换为结构:

octave:1> x = load_json (urlread ("http://headers.jsontest.com/"))
x =

  scalar structure containing the fields:

    X-Cloud-Trace-Context = 61910c1954c45fe021d35aeeb5477c20/2702800337619378204
    Host = headers.jsontest.com
    Accept = */*

octave:2> x.Host
ans = headers.jsontest.com

另一种方式:

octave:11> a.x = 5; a.y = {1,2,"foobar"}; a.z = rand(2); save_json (a)
ans = {
    "x": 5.0,
    "y": [
        1.0,
        2.0,
        "foobar"
    ],
    "z": [
        [
            0.6835708677160701,
            0.891779233104656
        ],
        [
            0.9378550691771155,
            0.664043049215685
        ]
    ]
}