1.介绍
JSON-Java是用于在Java中读取和写入JSON文档的参考应用程序。它是由Douglas Crockford编写的,他首先指定了JSON文档格式。它的工作原理是在POJO - 普通的旧Java对象中解析和存储JSON文本。这允许您的 Java 代码导航到文档,读取、更新和写入 JSON 文档。
2.依赖
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
3.快速入门
该项目的主要目的是展示如何在 Java 中解析和发出 JSON 文档。有两个主要类:
- JSONObject:从字符串中解析 JSON 对象,更新 JSON,并将其作为字符串发出。
- JSONArray:从字符串中解析 JSON 数组,更新 JSON,并将其作为字符串发出。
4.实战
1. JSONObject
package com.example.jsoup;
import lombok.Data;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.util.HashMap;
import java.util.Map;
public class JsonObject {
public static void main(String[] args) {
String jsonStr = "{\"edition\":\"third\",\"language\":\"java\",\"id\":\"07\",\"author\":\"张三\"}";
final JSONObject jsonObject = new JSONObject(jsonStr);
//获取所有的key
JSONArray names = jsonObject.names();
System.out.println(names);
final String id = jsonObject.getString("id");
System.out.println(id);
}
/**
* bean 转换为JSONObject
*/
public static JSONObject BeanToJson() {
BookModel2 book = new BookModel2();
book.setId("07");
book.setLanguage("java");
book.setEdition("third");
book.setAuthor("张三");
return new JSONObject(book);
}
/**
* 利用 JSONToKener 转JSON
*/
public static JSONObject jsonTokenerToJson() {
String jsonStr = "{\"edition\":\"third\",\"language\":\"java\",\"id\":\"07\",\"author\":\"张三\"}";
JSONTokener jsonTokener = new JSONTokener(jsonStr);
return new JSONObject(jsonTokener);
}
/**
* map转JSON
*/
public static JSONObject mapToJson() {
Map<String, String> map = new HashMap<>();
map.put("id", "07");
map.put("language", "java");
map.put("edition", "third");
map.put("author", "张三");
return new JSONObject(map);
}
/**
* bean转JSON name
*/
public static JSONObject namesToJson() {
BookModel book = new BookModel();
book.setId("07");
book.setLanguage("java");
book.setEdition("third");
book.setAuthor("张三");
String[] names = {"id", "language", "edition", "author"};
return new JSONObject(book, names);
}
/**
* 字符串直接转JSON
*/
public static JSONObject stringToJson() {
String jsonStr = "{\"edition\":\"third\",\"language\":\"java\",\"id\":\"07\",\"author\":\"张三\"}";
return new JSONObject(jsonStr);
}
/**
* JSON 添加元素
*/
public static JSONObject accumulateToJson() {
final JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("id", "07");
jsonObject.accumulate("language", "java");
jsonObject.accumulate("edition", "third");
jsonObject.accumulate("author", "张三");
return jsonObject;
}
/**
* JSON 添加元素
*/
public static JSONObject appendToJson() {
final JSONObject jsonObject = new JSONObject();
jsonObject.append("id", "07");
jsonObject.append("language", "java");
jsonObject.append("edition", "third");
jsonObject.append("author", "张三");
return jsonObject;
}
/**
* 创建一个bean 对象
*/
@Data
static class BookModel {
String id;
String language;
String edition;
String author;
}
@Data
static class BookModel2 {
private String id;
private String language;
private String edition;
private String author;
}
}
2. JSONArray
import org.json.JSONArray;
import org.json.JSONObject;
public class JsonObject {
public static void main(String[] args) {
String jsonStr = "[{\"edition\":\"third\",\"language\":\"java\",\"id\":\"07\",\"author\":\"张三\"}," +
"{\"edition\":\"one\",\"language\":\"C++\",\"id\":\"05\",\"author\":\"李四\"}]";
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length() ; i++) {
//获取每一个JSONArray 中每一个JSON数据
JSONObject jsonObject = jsonArray.getJSONObject(i);
final String id = jsonObject.getString("id");
System.out.println(id);
}
}
}