一、fastjson 的解析
package com.json;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.json.User;
/**
* @Title: Test001.java
* @Description: 返回 fastjson 解析
* @author HeLijin
* @date 2024-10-31 20:54:12
*/
public class Test001 {
// 定义json字符串
static String json = "{\"id\":\"001\",\"value\":\"File\",\"menuitem\":[{\"value\":\"New\",\"onclick\":\"CreateNewDoc()\"},{\"value\":\"Open\",\"onclick\":\"OpenDoc()\"},{\"value\":\"Close\",\"onclick\":\"CloseDoc()\"}]}";
public static void main(String[] args) {
// 方法一、先将字符串 转换成 jsonObject 对象
// parseObject() 方法
JSONObject jsonObject = new JSONObject().parseObject(json);
// getString() 方法根据key获取value
String id = jsonObject.getString("id");
String v1 = jsonObject.getString("value");
System.out.println("id:" + id + ",value:" +v1);
// getJSONArray() 方法
JSONArray jsonArray = jsonObject.getJSONArray("menuitem");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String value = object.getString("value");
String onclick = object.getString("onclick");
System.out.println("value:" + value +"--onclick:"+onclick);
}
//方法二、将json转化成对象 --常用
User user = new JSONObject().parseObject(json, User.class);
System.out.println(user.toString());
}
}
user实体类
package com.json;
import java.util.List;
/**
* @Title: User.java
* @Description:
* @author HeLijin
* @date 2024-10-31 21:42:15
*/
class Menuitem {
private String value;
private String onclick;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
@Override
public String toString() {
return "Menuitem [value=" + value + ", onclick=" + onclick + "]";
}
}
public class User {
private String id;
private String value;
private List<Menuitem> menuitem;
public User() {
super();
}
public User(String id, String value, List<Menuitem> menuitem) {
super();
this.id = id;
this.value = value;
this.menuitem = menuitem;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<Menuitem> getMenuitem() {
return menuitem;
}
public void setMenuitems(List<Menuitem> menuitem) {
this.menuitem = menuitem;
}
@Override
public String toString() {
return "User [id=" + id + ", value=" + value + ", menuitem=" + menuitem + "]";
}
}
二、fastjson 的java封装
package com.json;
import java.util.ArrayList;
import com.alibaba.fastjson.JSONObject;
/**
* @Title: Test002.java
* @Description: 自定义json 封装json字符串
* @author HeLijin
* @date 2024-10-31 22:18:27
*/
public class Test002 {
public static void main(String[] args) {
// 1.手动封装*****************************************************
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", "1");
jsonObject.put("name", "helijin");
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("itemId", "001");
jsonObject1.put("item", "唱歌");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("itemId", "002");
jsonObject2.put("item", "跳舞");
ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>();
arrayList.add(jsonObject1);
arrayList.add(jsonObject2);
jsonObject.put("items", arrayList);
System.out.println(jsonObject.toJSONString());
// 方法二 使用实体类封装json字符串**********************************
User user = new User();
user.setId("11");
user.setValue("xiaoming");
ArrayList<Menuitem> arrayList2 = new ArrayList<>();
Menuitem menuitem1 = new Menuitem();
menuitem1.setValue("0011");
menuitem1.setOnclick("哈哈哈");
Menuitem menuitem2 = new Menuitem();
menuitem2.setValue("0022");
menuitem2.setOnclick("嘻嘻嘻");
arrayList2.add(menuitem1);
arrayList2.add(menuitem2);
user.setMenuitems(arrayList2);
System.out.println(new JSONObject().toJSONString(user));
}
}