如何在Octave中解析json文件?

时间:2018-03-08 14:45:35

标签: json octave

与标题一样,是否有一种在Octave中解析/解码config.json文件的内置方法?

我已经阅读了Octave文档和不同的工具,但我发现的唯一一件就是这个工具: https://github.com/fangq/jsonlab

修改 目的是能够在两个不同的环境中使用相同的json配置文件:python和octave。那将是: 1.定义配置; 2.运行八度脚本,从config.json读取配置; 3.运行python脚本,从public class StockBarChart extends JPanel { private double[] values; private String[] names; private String title; public StockBarChart(double[] v, String[] n, String t) { names = n; values = v; title = t; } public void paintComponent(Graphics g) { super.paintComponent(g); if (values == null || values.length == 0) return; double minValue = 0; double maxValue = 0; for (int i = 0; i < values.length; i++) { if (minValue > values[i]) minValue = values[i]; if (maxValue < values[i]) maxValue = values[i]; } Dimension d = getSize(); int clientWidth = d.width; int clientHeight = d.height; int barWidth = clientWidth / values.length; Font titleFont = new Font("SansSerif", Font.BOLD, 20); FontMetrics titleFontMetrics = g.getFontMetrics(titleFont); Font labelFont = new Font("SansSerif", Font.PLAIN, 10); FontMetrics labelFontMetrics = g.getFontMetrics(labelFont); int titleWidth = titleFontMetrics.stringWidth(title); int y = titleFontMetrics.getAscent(); int x = (clientWidth - titleWidth) / 2; g.setFont(titleFont); g.drawString(title, x, y); int top = titleFontMetrics.getHeight(); int bottom = labelFontMetrics.getHeight(); if (maxValue == minValue) return; double scale = (clientHeight - top - bottom) / (maxValue - minValue); y = clientHeight - labelFontMetrics.getDescent(); g.setFont(labelFont); for (int i = 0; i < values.length; i++) { int valueX = i * barWidth + 1; int valueY = top; int height = (int) (values[i] * scale); if (values[i] >= 0) valueY += (int) ((maxValue - values[i]) * scale); else { valueY += (int) (maxValue * scale); height = -height; } g.setColor(Color.red); g.fillRect(valueX, valueY, barWidth - 2, height); g.setColor(Color.black); g.drawRect(valueX, valueY, barWidth - 2, height); int labelWidth = labelFontMetrics.stringWidth(names[i]); x = i * barWidth + (barWidth - labelWidth) / 2; g.drawString(names[i], x, y); } } public static void main(String[] argv) { try{ String breadStockBeforeTrim = new String(Files.readAllBytes(Paths.get("bread.txt"))); String breadStock = breadStockBeforeTrim.trim(); int breadNumber = Integer.parseInt(breadStock); String browniesStockBeforeTrim = new String(Files.readAllBytes(Paths.get("brownies.txt"))); String browniesStock = browniesStockBeforeTrim.trim(); int browniesNumber = Integer.parseInt(browniesStock); JFrame f = new JFrame(); f.setSize(400, 300); double[] values = new double[3]; String[] names = new String[3]; values[0] = 1; names[0] = "Item 1"; values[1] = breadNumber; names[1] = "Item 2"; values[2] = browniesNumber; names[2] = "Item 3"; f.getContentPane().add(new StockBarChart(values, names, "title")); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; f.addWindowListener(wndCloser); f.setVisible(true); }catch(IOException b){ b.printStackTrace(); } } } 读取配置;

我现在正在使用jsonlab工具集进行八度音阶,因为json非常简单,所以它的工作非常好。问题来自纯粹的好奇心,为什么octave默认不实现json序列化库。

所以,既然json很简单,我的解决方案就是使用https://github.com/fangq/jsonlab。从下面的评论来看,似乎并不适合与更复杂的jsons一起使用。

1 个答案:

答案 0 :(得分:1)

我已经在很长一段时间内对很多项目使用过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
        ]
    ]
}