有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中从JSON文件中提取值?

我是一个begginer程序员,我想做我自己的第一个小项目,从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数组并对其进行迭代,但迭代器无法工作(我不知道为什么)。我能够使用filereader和buffered reader(来自仅填充原始价格的simple.txt文件)将值输入到图形中,但不能使用JSON。那我到底怎么做呢

这是我关于stackoverflow的第一篇文章,正如我所说,我是一个初学者程序员,如果我的问题很愚蠢,我很抱歉,但我就是做不到,尽管我已经寻找了几个小时的解决方案,但没有一个对我有效

谢谢


共 (1) 个答案

  1. # 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();
        }