本教程仅限于 org.jsonv20201115 + jdk1.8

一、在pom.xml中加入org.json的依赖文件

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20201115</version>
</dependency>

二、书写代码,使用无参构造创建Json格式文本

public static String createJsonText(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","絷缘");
        jsonObject.put("age",23);
        jsonObject.put("gender","male");
        jsonObject.put("now_time",new SimpleDateFormat("yyyy-MM-dd kk:mm:ss").format(new Date(System.currentTimeMillis())));
        jsonObject.put("skills",new String[]{"Java","Linux","JavaScript","Vue","SemanticUI","NodeJS"});
        jsonObject.put("has_house",false);
        jsonObject.put("has_car",false);
        return jsonObject.toString();
}

三、书写代码,使用有参构造传入HashMap对象创建Json格式文本

public static String createJsonByMap(){
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("name","絷缘");
        hashMap.put("age",23);
        hashMap.put("gender","male");
        hashMap.put("now_time",new SimpleDateFormat("yyyy-MM-dd kk:mm:ss").format(new Date(System.currentTimeMillis())));
        hashMap.put("skills",new String[]{"Java","Linux","JavaScript","Vue","SemanticUI","NodeJS"});
        hashMap.put("has_house",false);
        hashMap.put("has_car",false);
        JSONObject jsonObject = new JSONObject(hashMap);
        return jsonObject.toString();
    }

四、书写代码,封装JavaBean实现创建Json格式文本

import java.util.Arrays;

public class Person {
    private String name;
    private int age;
    private String gender;
    private boolean has_house;
    private boolean has_car;
    private String [] skills;
    public Person(){}
    public Person(String name, int age, String gender, boolean has_house, boolean has_car, String[] skills) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.has_house = has_house;
        this.has_car = has_car;
        this.skills = skills;
    }
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public boolean isHas_house() {
        return has_house;
    }

    public boolean isHas_car() {
        return has_car;
    }

    public String[] getSkills() {
        return skills;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setHas_house(boolean has_house) {
        this.has_house = has_house;
    }

    public void setHas_car(boolean has_car) {
        this.has_car = has_car;
    }

    public void setSkills(String[] skills) {
        this.skills = skills;
    }
}
public static String createJsonByBean(){
        Person person = new Person("絷缘", 23, "male", false, false, new String[]{"Java", "Linux", "JavaScript", "Vue", "SemanticUI", "NodeJS"});
        JSONObject jsonObject = new JSONObject(person);
        return jsonObject.toString();
    }

书写代码,实现解析Json文本

需要自己创建文件person.json置于src目录(普通项目)或resources目录下(Maven项目)

{
  "skills": [
    "Java",
    "Linux",
    "JavaScript",
    "Vue",
    "SemanticUI",
    "NodeJS"
  ],
  "has_car": false,
  "gender": "male",
  "name": "絷缘",
  "has_house": false,
  "age": 23
}

为了方便看代码,文章中出现的异常我都会抛出

public static void parseJson() throws IOException {
        InputStream is = JsonDemo1.class.getClassLoader().getResourceAsStream("person.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String content;
        while((content=br.readLine())!=null){
            sb.append(content);
        }
        System.out.println(sb.toString());
        JSONObject jsonObject = new JSONObject(sb.toString());
        System.out.println(jsonObject.getString("name"));
        System.out.println(jsonObject.getJSONArray("skills"));
        System.out.println(jsonObject.getString("gender"));
        System.out.println(jsonObject.getInt("age"));
        System.out.println(jsonObject.getBoolean("has_house"));
        System.out.println(jsonObject.getBoolean("has_car"));
    }

原文作者:絷缘
作者邮箱:zhiyuanworkemail@163.com
原文地址:https://zhiyuandnc.github.io/4riCXnG78/
版权声明:本文为博主原创文章,转载请注明原文链接作者信息