在java中读取包含多个对象的json文件

在java中读取包含多个对象的json文件,java,json,jackson,Java,Json,Jackson,我编写了一个程序,可以读取一个简单的json文件: public static void main(String[] args) { JSONParser parser = new JSONParser(); try { JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Zonoid/Desktop/EQ.json")); for (Object o : a)

我编写了一个程序,可以读取一个简单的json文件:

public static void main(String[] args) {
    JSONParser parser = new JSONParser();
    try {
        JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Zonoid/Desktop/EQ.json"));
        for (Object o : a)
        {
            JSONObject obj = (JSONObject) o;
            String city = (String) obj.get("CITY");
            System.out.println("City : " + city);
            String loc = (String) obj.get("LOCATION");
            System.out.println("Location : " + loc);
            long el = (Long) obj.get("E_LEVEL");
            System.out.println("Emergency Level : " + el);
            long depth = (Long) obj.get("DEPTH");
            System.out.println("Depth : " + depth);
            long i = (Long) obj.get("INTENSITY");
            System.out.println("Intensity :"+i);
            System.out.println("\n");
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
我的json文件是:

[{"CITY":"MUMBAI","LOCATION":"a" ,"E_LEVEL": 6,"DEPTH":10,"INTENSITY":5},
{"CITY":"MUMBAI","LOCATION":"b" ,"E_LEVEL": 8,"DEPTH":20,"INTENSITY":4},
{"CITY":"MUMBAI","LOCATION":"c" ,"E_LEVEL": 3,"DEPTH":13,"INTENSITY":5},
{"CITY":"MUMBAI","LOCATION":"d" ,"E_LEVEL": 6,"DEPTH":12,"INTENSITY":4},]
我正在从事一个处理地震警报的项目,希望读取它们的JSON文件,但我无法将它们导入JSON数组。我要导入的文件如下所示:

{
  "type": "FeatureCollection",
  "metadata": {
    "generated": 1488472809000,
    "url": "https:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/summary\/significant_week.geojson",
    "title": "USGS Significant Earthquakes, Past Week",
    "status": 200,
    "api": "1.5.4",
    "count": 2
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "mag": 5.5,
        "place": "42km WSW of Anchor Point, Alaska",
        "time": 1488420690658,....

请说明应进行哪些更改。

如果您试图仅从功能读取,首先需要将整个文件作为对象读取。然后,您可以按以下方式读取阵列部分:

Object object = parser.parse(new FileReader("C:/Users/Zonoid/Desktop/EQ.json"));
JSONObject jasonObject = (JSONObject) object;
JSONArray features = (JSONArray) jasonObject.get("features");

如果试图仅从要素读取,首先需要将整个文件作为对象读取。然后,您可以按以下方式读取阵列部分:

Object object = parser.parse(new FileReader("C:/Users/Zonoid/Desktop/EQ.json"));
JSONObject jasonObject = (JSONObject) object;
JSONArray features = (JSONArray) jasonObject.get("features");

您发布的代码有什么问题吗?它给出了一个错误org.json.simple.JSONObject不能在practice.json.main(json.java:17)上转换为org.json.simple.JSONArray。您需要更改json文件。json文件包含一个对象(以{开头,以}结尾)。您希望它是一个对象数组(以[{开头,以}结尾)首先,如果我是正确的,你正在使用
Udacity项目应用程序
,从网站上得到的是JSON对象,因为你有一个features数组,你应该从该数组中提取对象,而不是直接从文件中的数据中提取对象。我尝试过,但没有删除错误。你发布的代码有什么问题?它给出了错误组织。我现在明白了,json.simple.JSONObject不能在practice.json.main(json.java:17)上转换为org.json.simple.JSONArray。您需要更改json文件。json文件包含一个对象(以{开头,以}结尾)。您希望它是一个对象数组(以[{开头,以}结尾)。首先,如果我是正确的,您正在使用
Udacity项目应用程序
,从网站上得到的是JSON对象,因为您有features数组,您应该从中提取对象,而不是直接从文件中的数据提取对象。我尝试了,但没有消除错误OK,我的错误是直接使用JSONObject和parser.pars谢谢你的帮助OK,我的错误是直接将JSONObject与parser.parse一起使用,而不进行转换。谢谢你的帮助