猿问

如何使用简单的JSON库将JSON文件读入Java

我想JSON使用json简单库使用Java 读取此文件。


我的JSON文件如下所示:


[  

    {  

        "name":"John",

        "city":"Berlin",

        "cars":[  

            "audi",

            "bmw"

        ],

        "job":"Teacher"

    },

    {  

        "name":"Mark",

        "city":"Oslo",

        "cars":[  

            "VW",

            "Toyata"

        ],

        "job":"Doctor"

    }

]

这是我为读取此文件而编写的Java代码:


package javaapplication1;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Iterator;

import org.json.simple.JSONArray;

import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;

import org.json.simple.parser.ParseException;


public class JavaApplication1 {

    public static void main(String[] args) {


        JSONParser parser = new JSONParser();


        try {     

            Object obj = parser.parse(new FileReader("c:\\file.json"));


            JSONObject jsonObject =  (JSONObject) obj;


            String name = (String) jsonObject.get("name");

            System.out.println(name);


            String city = (String) jsonObject.get("city");

            System.out.println(city);


            String job = (String) jsonObject.get("job");

            System.out.println(job);


            // loop array

            JSONArray cars = (JSONArray) jsonObject.get("cars");

            Iterator<String> iterator = cars.iterator();

            while (iterator.hasNext()) {

             System.out.println(iterator.next());

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ParseException e) {

            e.printStackTrace();

        }

    }

}

但是我得到以下异常:


线程“主”中的异常java.lang.ClassCastException:org.json.simple.JSONArray无法在javaapplication1.JavaApplication1.main(JavaApplication1.java:24)处转换为org.json.simple.JSONObject。


有人可以告诉我我做错了吗?整个文件是一个数组,并且文件的整个数组中包含对象和另一个数组(汽车)。但是我不知道如何将整个数组解析为java数组。我希望有人可以帮助我解决我的代码中缺少的代码行。


谢谢


侃侃无极
浏览 579回答 3
3回答

偶然的你

您可以使用jackson库,只需使用这3行代码即可将json文件转换为Java Object。ObjectMapper mapper = new ObjectMapper();InputStream is = Test.class.getResourceAsStream("/test.json");testObj = mapper.readValue(is, Test.class);

慕容708150

从JsonFile读取public static ArrayList<Employee> readFromJsonFile(String fileName){&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Employee> result = new ArrayList<Employee>();&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject obj = new JSONObject(text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray arr = obj.getJSONArray("employees");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < arr.length(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = arr.getJSONObject(i).getString("name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; short salary = Short.parseShort(arr.getJSONObject(i).getString("salary"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String position = arr.getJSONObject(i).getString("position");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte years_in_company = Byte.parseByte(arr.getJSONObject(i).getString("years_in_company"));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (position.compareToIgnoreCase("manager") == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(new Manager(name, salary, position, years_in_company));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(new OrdinaryEmployee(name, salary, position, years_in_company));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception ex){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答