来自蚂蚁课堂。
1.项目添加依赖。
<!-- 添加阿里巴巴解析json工具类 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
2.JSONTest.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JSONTest {
public static void main(String[] args) {
// setJSON();
// setFastJSON();
// setMapToJSON();
// setBeanToJSON();
// analysisJson();
jsonToBean();
}
/**
* 通过传统方式自己拼接字符串JSON
*/
public static void setJSON() {
String str = " { \"errorCode\": \"0\",\"errorMsg\": \"调用接口成功\",\"data\": [{\"userName\": \"余胜军\",\"position\": \"蚂蚁课堂创始人\",\"webAddres\": \"www.itmayiedu.com\" },{ \"userName\": \"周安旭\",\"position\": \"蚂蚁课堂合伙人\",\"webAddres\": \"www.itmayiedu.com\" } ]}";
System.out.println(str);
}
/**
* 通过fastJSON封装JSON
*/
public static void setFastJSON() {
JSONObject root = new JSONObject();
root.put("errorCode",0);
root.put("errorMsg","调用接口成功");
JSONArray dataArr = new JSONArray();
JSONObject userYushengjun = new JSONObject();
userYushengjun.put("userName","余胜军");
userYushengjun.put("position","蚂蚁课堂创始人");
userYushengjun.put("webAddres","www.itmayiedu.com");
dataArr.add(userYushengjun);
root.put("data",dataArr);
System.out.println(root.toJSONString());
}
/**
* 通过map转换成json
*/
static public void setMapToJSON() {
HashMap<String,Object> root = new HashMap<String,Object>();
root.put("errorCode","调用接口成功");
List<Map<String,String>> dataArr = new ArrayList<Map<String,String>>();
Map<String,String> userYushengjun = new HashMap<String,String>();
userYushengjun.put("userName","www.itmayiedu.com");
Map<String,String> itmayiedu = new HashMap<String,String>();
itmayiedu.put("userName","余胜军1");
itmayiedu.put("position","蚂蚁课堂创始人1");
itmayiedu.put("webAddres","www.itmayiedu.com");
dataArr.add(itmayiedu);
dataArr.add(userYushengjun);
root.put("data",dataArr);
System.out.println(new JSONObject().toJSONString(root));
}
/**
* 通过实体类转换JSON
*/
static public void setBeanToJSON() {
RootEntity rootEntity = new RootEntity();
rootEntity.setErrorCode("0");
rootEntity.setErrorMsg("调用接口成功");
List<UserEntity> data = new ArrayList<UserEntity>();
UserEntity userEntity = new UserEntity();
userEntity.setPosition("蚂蚁课堂创始人");
userEntity.setUserName("余胜军");
userEntity.setWebAddres("itmayiedu.com");
data.add(userEntity);
rootEntity.setData(data);
System.out.println(new JSONObject().toJSONString(rootEntity));
}
/**
* 解析JSON
*/
static public void analysisJson() {
String jsonStr = "{ \"errorCode\": \"0\",\"webAddres\": \"www.itmayiedu.com\" } ]}";
// 将json字符串转换成json
JSONObject root = new JSONObject().parSEObject(jsonStr);
String errorCode = root.getString("errorCode");
String errorMsg = root.getString("errorMsg");
System.out.println("errorCode:" + errorCode + ",errorMsg:" + errorMsg);
JSONArray dataArr = root.getJSONArray("data");
for (int i = 0; i < dataArr.size(); i++) {
JSONObject dataBean = (JSONObject) dataArr.get(i);
String position = dataBean.getString("position");
String userName = dataBean.getString("userName");
String webAddres = dataBean.getString("webAddres");
System.out.println("position:" + position + ",userName:" + userName + ",webAddres:" + webAddres);
}
}
/**
* 通过JSON转实体类
*/
static public void jsonToBean() {
String jsonStr = "{ \"errorCode\": \"0\",\"webAddres\": \"www.itmayiedu.com\" } ]}";
// 将json字符串转换成json
RootEntity rootEntity = new JSONObject().parSEObject(jsonStr,RootEntity.class);
System.out.println(rootEntity.toString());
}
}
3.RootEntity.java
private String errorCode;
private String errorMsg;
private List<UserEntity> data;
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public List<UserEntity> getData() {
return data;
}
public void setData(List<UserEntity> data) {
this.data = data;
}
@Override
public String toString() {
return "RootEntity [errorCode=" + errorCode + ",errorMsg=" + errorMsg + ",data=" + data + "]";
}
4.UserEntity.java
public class UserEntity { private String position; private String userName; private String webAddres; public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getWebAddres() { return webAddres; } public void setWebAddres(String webAddres) { this.webAddres = webAddres; } @Override public String toString() { return "UserEntity [position=" + position + ",userName=" + userName + ",webAddres=" + webAddres + "]"; }