【问题标题】:How to extract values from JSON file in java? [duplicate]java - 如何从Java中的JSON文件中提取值? [复制]
【发布时间】:2020-10-21 00:36:38
【问题描述】:

我是一个初学者程序员,我正在尝试制作我自己的第一个小项目,从 JSON 文件中“提取”值,然后通过它在 javafx 折线图上绘制它。我试图找到一个解决方案来解决我从 JSON 文件中提取值的问题(使用 json.simple 或/和 json.org),但我仍然无法正确地做到这一点。

我的json文件里面是:

[
  {
    "Price": 7999,
    "Id": 0,
    "Datetime": "2020-06-09T21:55:55.335Z"
  },
  {
    "Price": 7500,
    "Id": 1,
    "Datetime": "2020-06-10T21:55:55.335Z"
  },
  {
    "Price": 7900,
    "Id": 2,
    "Datetime": "2020-06-11T21:55:55.335Z"
  },
  {
    "Price": 6800,
    "Id": 3,
    "Datetime": "2020-06-12T21:55:55.335Z"
  },
  {
    "Price": 6949,
    "Id": 4,
    "Datetime": "2020-06-13T21:55:55.335Z"
  },
  {
    "Price": 9160,
    "Id": 5,
    "Datetime": "2020-06-14T21:55:55.335Z"
  },
  {
    "Price": 7500,
    "Id": 6,
    "Datetime": "2020-06-15T21:55:55.335Z"
  },
  {
    "Price": 6999,
    "Id": 7,
    "Datetime": "2020-06-16T21:55:55.335Z"
  },
  {
    "Price": 7259,
    "Id": 8,
    "Datetime": "2020-06-17T21:55:55.335Z"
  },
  {
    "Price": 7259,
    "Id": 9,
    "Datetime": "2020-06-18T21:55:55.335Z"
  },
  {
    "Price": 6700,
    "Id": 10,
    "Datetime": "2020-06-19T21:55:55.335Z"
  },
  {
    "Price": 5999,
    "Id": 11,
    "Datetime": "2020-06-20T21:55:55.335Z"
  },
  {
    "Price": 5500,
    "Id": 12,
    "Datetime": "2020-06-21T21:55:55.335Z"
  },
  {
    "Price": 6200,
    "Id": 13,
    "Datetime": "2020-06-22T21:55:55.335Z"
  },
  {
    "Price": 6260,
    "Id": 14,
    "Datetime": "2020-06-23T21:55:55.335Z"
  },
  {
    "Price": 5800,
    "Id": 15,
    "Datetime": "2020-06-24T21:55:55.335Z"
  },
  {
    "Price": 5300,
    "Id": 16,
    "Datetime": "2020-06-25T21:55:55.335Z"
  },
  {
    "Price": 5090,
    "Id": 17,
    "Datetime": "2020-06-26T21:55:55.335Z"
  },
  {
    "Price": 4999,
    "Id": 18,
    "Datetime": "2020-06-27T21:55:55.335Z"
  },
  {
    "Price": 4999,
    "Id": 19,
    "Datetime": "2020-06-28T21:55:55.335Z"
  }
]

我有兴趣从中提取“价格”和“日期时间”值。我试图使用 JSON 数组并遍历它,但迭代器无法工作(我不知道为什么)。我能够使用文件读取器和缓冲读取器(来自仅填充原始价格的简单 .txt 文件)将值转换为图形,但不能来自 JSON。那我到底该怎么做呢?

这是我在 stackoverflow 上的第一篇文章,正如我所说,我是一名初学者程序员,如果我的问题很愚蠢,我很抱歉,但我就是无法做到,即使我已经寻找了几个小时的解决方案,但他们都没有为我工作。

谢谢

【问题讨论】:

  • 你是如何阅读这个 JSON 文件的?
  • 这能回答你的问题吗? Converting JSON data to Java object
  • google 的这个库很棒:github.com/google/gson
  • 嗨,我正在尝试使用 JSONParser
  • 我的 src 中有一个 JSON 文件:private File file = new File("src\\data.json");

标签: java json


【解决方案1】:

有许多可用的开源库可以帮助您。

但既然你提到使用 JsonSimple ('com.googlecode.json-simple:json-simple:1.1.1'),这里有两种方法:

try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) {
            // read the json file
 
 
            JSONParser jsonParser = new JSONParser();
            JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
 
            Iterator i = jsonArray.iterator();
 
            // take each value from the json array separately
            while (i.hasNext()) {
                JSONObject innerObj = (JSONObject) i.next();
                System.out.println("price " + innerObj.get("Price") +
                        " with id " + innerObj.get("Id"));
            }
}

For 循环方法:(由于使用迭代器,您会遇到问题)

try (FileReader reader = new FileReader(ClassLoader.getSystemResource(filePath).getFile())) {
      JSONParser jsonParser = new JSONParser();
      JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
      for(Object o: jsonArray){
        if ( o instanceof JSONObject) {
          JSONObject jsonObject = (JSONObject) o;
          System.out.println("price " + jsonObject.get("Price") +
              " with id " + jsonObject.get("Id"));
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

【讨论】:

  • 有一个小问题,我不能(由于某种原因)使用迭代器,因为它说它不能解决方法。
  • @Insekure 我也添加了for循环方式..请试一试。
  • 好的,所以我尝试了它 - 它有点工作,但是在我将 JSONArray 的类型更改为 JSON.simple 之后(由于某种原因,我不会删除我的 org.Json 库,因为它只是表明foreach 不适用于 org.jsonArray。我在 try(FileReader . . . . .) 中得到了 nullpointer 异常,也许 org.lib 弄乱了代码?无论如何,非常感谢您的回答,但我仍然需要弄清楚是什么弄乱了代码...
  • 导入 org.json.simple.JSONArray;导入 org.json.simple.JSONObject;导入 org.json.simple.parser.JSONParser;这些是您需要的导入。
  • 是的,在尝试时仍然出现空指针异常 (FileReader reader = new FileReader(ClassLoader.getSystemResource("src\\data.json").getFile()))。我怎样才能上传代码?也许课堂本身有问题。我正在使用 Controller 处理 javafx 模板,并在一个单独的类中进行。但它不应该搞砸任何事情
猜你喜欢
  • 1970-01-01
  • 2016-02-11
  • 2020-11-26
  • 2021-03-28
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
相关资源
最近更新 更多