java讀取JSON文件轉為對象

1、創(chuàng)建JSON.txt文件
{
    "name": "小王",
    "age": 23,
    "sex": "男"
}
2、創(chuàng)建Person對象
@Data
public class Person implements Serializable {
    private static final long serialVersionUID = -348549485910717452L;

    private String name;
    private String sex;
    private Integer age;
}
3、讀取JSON文件獲取字符串
public static String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long fileLength = file.length();
        byte[] fileContent = new byte[fileLength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(fileContent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(fileContent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }
4、讀取JSON文件獲取字符串,并轉為對象
public static JSONObject readTxtToJson(String txt) {
        File file = new File(txt);
        String json = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(file); // 文件輸入流
            isr = new InputStreamReader(fis,"utf-8"); // 轉為字符流
            br = new BufferedReader(isr); // 轉為緩沖字符流
            String line = "";
            //使用readLine方法,一次讀一行
            while ((line = br.readLine()) != null) {
                json += line;
            }
            System.out.println(json);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return JSONObject.fromObject(json);
    }
5、調用方法
public static void main(String[] args) {
        String txt = readToString("/Users/admin/Documents/spring.txt");
        System.err.println(txt);

        JSONObject jsonObject = readTxtToJson("/Users/admin/Desktop/json.txt");
        String name = (String) jsonObject.get("name");
        Person person = (Person) JSONObject.toBean(jsonObject, Person.class);
        System.out.println(name);
        System.err.println(person);

    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容