Jackson有1.x系列和2.x系列,2.x系列有3个jar包需要下载:
jackson-core-2.2.3.jar(核心jar包)
jackson-annotations-2.2.3.jar(该包提供Json注解支持)
jackson-databind-2.2.3.jar (包含以上两个)
maven依赖
com.fasterxml.jackson.core
jackson-databind
2.5.3
由于篇幅和时间原因,这里不再写model,都用list,map代替
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* json解析测试
*
* @Author: Mr_li
* @CreateDate: 2018-11-07$ 14:48$
* @Version: 1.0
*/
public class JackSonTestCase {
private static ObjectMapper mapper = new ObjectMapper();
/**
* 数据准备
*/
static class getData {
Map map = new HashMap();
List list = new ArrayList<>();
List
fastJson对于json格式字符串的解析主要用到了一下三个类:
JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
JSONObject:fastJson提供的json对象。
JSONArray:fastJson提供json数组对象。
maven依赖
com.alibaba
fastjson
1.2.51
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* json解析测试
*
* @Author: Mr_li
* @CreateDate: 2018-11-07$ 14:48$
* @Version: 1.0
*/
public class FastjsonTestCase {
private static JSONObject jsonObject = new JSONObject();
private static JSONArray jsonArr = new JSONArray();
class Student {
private String studentName;
private Integer studentAge;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
}
/**
* 数据准备
*/
static class getData {
Map map = new HashMap();
List list = new ArrayList<>();
List> listMap = new ArrayList>();
public Map getMap() {
map.put("name", "张三");
map.put("age", "18");
return map;
}
public List getList() {
list.add("a");
list.add("b");
return list;
}
public List> getListMap() {
listMap.add(getMap());
return listMap;
}
}
public static void test() throws IOException {
//jsonStr-->>jsonObj
String json1 = "{\"studentName\":\"lily\",\"studentAge\":12}";
JSONObject jsonObject = JSON.parseObject(json1);
//jsonStr-->>jsonArray
String json2 = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
JSONArray jsonArray = JSON.parseArray(json1);
//jsonstr-->>javaObj
Student student = JSON.parseObject(json2, Student.class);
List list = new ArrayList();
ArrayList list1 = (ArrayList) JSONObject.parseObject(json2, List.class);
//javaObj-->>jsonStr
String jsonString = JSON.toJSONString(student);
}
public static void main(String[] args) throws IOException {
FastjsonTestCase.test();
}
}
目前网络上有很多接口是Json形式的,Gson是谷歌开源的Json解析库,可以方便的将Java对象转换成Json字符串,也可以方便的将Json字符串转换成Java对象。
com.google.code.gson
gson
2.8.5
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* json解析测试
*
* @Author: Mr_li
* @CreateDate: 2018-11-07$ 14:48$
* @Version: 1.0
*/
public class GsonTestCase {
private static Gson gson = new Gson();
private static JsonParser parser = new JsonParser();
class Student {
private String studentName;
private Integer studentAge;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
}
/**
* 数据准备
*/
static class getData {
Map map = new HashMap();
List list = new ArrayList<>();
List> listMap = new ArrayList>();
public Map getMap() {
map.put("name", "张三");
map.put("age", "18");
return map;
}
public List getList() {
list.add("a");
list.add("b");
return list;
}
public List> getListMap() {
listMap.add(getMap());
return listMap;
}
}
public static void test() throws IOException {
//jsonStr-->>jsonObj
String json1 = "{\"studentName\":\"lily\",\"studentAge\":12}";
JsonObject jsonObject = (JsonObject) parser.parse(json1);
JsonElement parse = parser.parse(json1);
JsonObject asJsonObject = parse.getAsJsonObject();
//jsonStr-->>jsonArray
String json2 = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
JsonArray jsonArray = (JsonArray) parser.parse(json1);
JsonElement parse1 = parser.parse(json1);
JsonArray asJsonArray = parse1.getAsJsonArray();
//jsonStr-->>javaObj
Student student = new Gson().fromJson(json1, Student.class);
HashMap studentHashMap = gson.fromJson(json1, new TypeToken>() {
}.getType());
//javaObj-->>jsonStr
String json = gson.toJson(student);
}
public static void main(String[] args) throws IOException {
GsonTestCase.test();
}
}
perfect!!!