欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读写json文件

程序员文章站 2022-03-03 14:48:06
...
写
//生成json文件
        Person person=new Person("张三",18);
        Writer writer= null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream("E:\\a.json"),"utf-8");
            JSONWriter jsonWriter = new JSONWriter(writer);
            jsonWriter.writeObject(person);
            jsonWriter.close();
            writer.close();
        } catch (Exception e) {
          e.printStackTrace();
        }

读
//读取json文件
        Reader reader=null;
        try {
            reader=new InputStreamReader(new FileInputStream("E:\\a.json"),"utf-8");
            JSONReader jsonReader=new JSONReader(reader);
            //1、
            //Person person1=new Person();
            //jsonReader.readObject(person1);
            //System.out.println("person1:"+person1);
            //2、
            Person person2 = jsonReader.readObject(Person.class);
            System.out.println("person2:"+person2);
            jsonReader.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

 

相关标签: json