一,FastJson
1,简介
FastJson是阿里的开源框架,是一个用Java语言编写的功能完善的高性能JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。
FastJson对json格式字符串的解析主要用到了3个类:
JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换;
JSONObject:fastJson提供的json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value};
JSONArray:fastJson提供json数组对象,使用中括号[ ],只不过数组里面的项也是json键值对格式的。
2,Json格式字符串与JSONObject之间的相互转换
2.1 将JSONObject转化为Json字符串
import com.alibaba.fastjson.JSONObject;
/**
* 将Json对象转为json字符串
*/
public class JsonOper {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "帽峰山");
jsonObject.put("address", "白云区");
String str = jsonObject.toJSONString();
System.out.println(str);
}
}
// 输出:{"address":"白云区","name":"帽峰山"}
2.2 将Json字符串转为JSONObject
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* 将Json字符串转为json对象
*/
public class JsonOper {
public static void main(String[] args) {
String str = "{\"address\":\"白云区\",\"name\":\"帽峰山\"}";
JSONObject jsonObject = JSON.parseObject(str);
System.out.println(jsonObject.getString("name") + ":" + jsonObject.getString("address"));
}
}
//输出:帽峰山:白云区
3,Json格式字符串与JSONArray之间的相互转换
3.1 将JSONArray转为Json字符串
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 将Json数组转为json字符串
*/
public class JsonOper {
public static void main(String[] args) {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name", "帽峰山");
jsonObject1.put("address", "白云区");
jsonArray.add(jsonObject1);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", "莲花山");
jsonObject2.put("address", "番禺区");
jsonArray.add(jsonObject2);
String str = jsonArray.toJSONString();
System.out.println(str);
}
}
// 输出:[{"address":"白云区","name":"帽峰山"},{"address":"番禺区","name":"莲花山"}]
3.2 将json字符串转为JSONArray
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 将Json字符串转为json数组
*/
public class JsonOper {
public static void main(String[] args) {
String str = "[{\"address\":\"白云区\",\"name\":\"帽峰山\"},{\"address\":\"番禺区\",\"name\":\"莲花山\"}]";
JSONArray jsonArray = JSON.parseArray(str);
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
System.out.println(jsonObject.getString("name")+":"+jsonObject.getString("address"));
}
}
}
输出:
帽峰山:白云区
莲花山:番禺区
4,复杂json格式字符串与JSONObject之间的转化
4.1 将JSONObject格式转为复杂json字符串
import com.alibaba.fastjson.JSONObject;
/**
* 将JSONObject格式转为复杂json字符串
*/
public class JsonOper {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
// vaule为字符串
jsonObject.put("name", "帽峰山");
jsonObject.put("address", "白云区");
// value为对象
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", "黄茶园山");
jsonObject2.put("address", "从化");
jsonObject.put("object", jsonObject2);
// value为数组
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject3 = new JSONObject();
jsonObject3.put("name", "凤凰山");
jsonObject3.put("address", "白云区");
JSONObject jsonObject4 = new JSONObject();
jsonObject4.put("name", "猪仔山");
jsonObject4.put("address", "白云区");
jsonArray.add(jsonObject3);
jsonArray.add(jsonObject4);
jsonObject.put("array", jsonArray);
String str = jsonObject.toJSONString();
System.out.println(str);
}
}
输出:{"address":"白云区","array":[{"address":"白云区","name":"凤凰山"},{"address":"白云区","name":"猪仔山"}],"name":"帽峰山","object":{"address":"从化","name":"黄茶园山"}}
4.2 将复杂json字符串转为JSONObject
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* 将复杂Json字符串转为json对象
*/
public class JsonOper {
public static void main(String[] args) {
String str = "{\"address\":\"白云区\",\"array\":[{\"address\":\"白云区\",\"name\":\"凤凰山\"},{\"address\":\"白云区\",\"name\":\"猪仔山\"}],\"name\":\"帽峰山\",\"object\":{\"address\":\"从化\",\"name\":\"黄茶园山\"}}";
JSONObject jsonObject = JSON.parseObject(str);
String name = jsonObject.getString("name");
String address = jsonObject.getString("address");
JSONObject jsonObject2 = jsonObject.getJSONObject("object");
JSONArray jsonArray = jsonObject.getJSONArray("array");
}
}
5,Json格式字符串与JavaBean之间的相互转化。
json字符串与javaBean之间的转换推荐使用 TypeReference 这个类,使用泛型可以更加清晰,当然也有其它的转换方式
首先提供3个JavaBean
/**
* 山
*/
public class Mountain {
private String name;
private String address;
private ViewPoint viewPoint;
private List<Route> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public ViewPoint getViewPoint() {
return viewPoint;
}
public void setViewPoint(ViewPoint viewPoint) {
this.viewPoint = viewPoint;
}
public List<Route> getList() {
return list;
}
public void setList(List<Route> list) {
this.list = list;
}
}
/**
* 景点
*/
public class ViewPoint {
private String viewPointName;
private String viewPointPrice;
public String getViewPointName() {
return viewPointName;
}
public void setViewPointName(String viewPointName) {
this.viewPointName = viewPointName;
}
public String getViewPointPrice() {
return viewPointPrice;
}
public void setViewPointPrice(String viewPointPrice) {
this.viewPointPrice = viewPointPrice;
}
}
/**
* 线路
*/
public class Route {
private String routeName;
private Integer routeLength;
public String getRouteName() {
return routeName;
}
public void setRouteName(String routeName) {
this.routeName = routeName;
}
public Integer getRouteLength() {
return routeLength;
}
public void setRouteLength(Integer routeLength) {
this.routeLength = routeLength;
}
}
5.1 json字符串与简单JavaBean之间的转化
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
/**
* 将Json字符串转为简单JavaBean对象
*/
public class JsonOper {
public static void main(String[] args) {
String str = "{\"address\":\"白云区\",\"name\":\"帽峰山\"}";
Mountain mountain = JSON.parseObject(str, new TypeReference<Mountain>(){});
System.out.println(mountain.getName() + ":" + mountain.getAddress());
}
}
5.2 json字符串与集合JavaBean之间的转化
/**
* 将Json字符串转为JavaBean集合
*/
public class JsonOper {
public static void main(String[] args) {
String str = "[{\"address\":\"白云区\",\"name\":\"凤凰山\"},{\"address\":\"白云区\",\"name\":\"猪仔山\"}]";
List<Mountain> mountains = JSON.parseObject(str, new TypeReference<ArrayList<Mountain>>(){});
for (Mountain mountain : mountains) {
System.out.println(mountain.getName() + ":" + mountain.getAddress());
}
}
}
5.3 复杂json字符串与JavaBean之间的转化
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
/**
* 将复杂Json字符串转为JavaBean
*/
public class JsonOper {
public static void main(String[] args) {
String str = "{\"address\":\"番禺区\",\"viewPoint\":{\"viewPointName\":\"望海观音\",\"viewPointPrice\":\"54\"},\"name\":\"莲花山\",\"list\":[{\"routeLength\":33,\"routeName\":\"火帽线\"},{\"routeLength\":25,\"routeName\":\"牛木内线\"}]}";
Mountain mountain = JSON.parseObject(str, new TypeReference<Mountain>(){});
String name = mountain.getName();
String address = mountain.getAddress();
ViewPoint viewPoint = mountain.getViewPoint();
List<Route> list = mountain.getList();
}
}
好了,关于FastJson的学习暂时到这里了,记下以免生疏。