JAVA如何获取json字符串 最好有实例 感谢
发布网友
发布时间:2022-05-06 14:15
我来回答
共2个回答
热心网友
时间:2022-04-23 10:02
//bean类
public class Tet {
private String buildingNum;//"E1",
private String className;//"1",
private String classNum;//"通信15-1班",
private String college;//"电子信息工程学院",
private String dormNum;//"110",
private String grade;//"2015",
private String isCommunist;//"0",
private String isDormLeader;//"1",
private String isStudentLeader;//"1",
private String major;//"通信工程",
private String phoneNum;//"13591991111",
private String status;//"1",
private String studentId;//"1",
private String studentName;//"张三",
private String teachername;//"张四"
public String getBuildingNum() {
return buildingNum;
}
public void setBuildingNum(String buildingNum) {
this.buildingNum = buildingNum;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getDormNum() {
return dormNum;
}
public void setDormNum(String dormNum) {
this.dormNum = dormNum;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getIsCommunist() {
return isCommunist;
}
public void setIsCommunist(String isCommunist) {
this.isCommunist = isCommunist;
}
public String getIsDormLeader() {
return isDormLeader;
}
public void setIsDormLeader(String isDormLeader) {
this.isDormLeader = isDormLeader;
}
public String getIsStudentLeader() {
return isStudentLeader;
}
public void setIsStudentLeader(String isStudentLeader) {
this.isStudentLeader = isStudentLeader;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
}
//实现类
import net.sf.json.JSONArray;
public class Test {
public static void main(String[] args) {
/*
* 做法 建立一个bean类 属性就为 字符串的 Key ;
*/
//定义JSON串 String 类型
String a="[{\"buildingNum\":\"E1\",\"className\":\"1\",\"classNum\":\"通信15-1班\",\"college\":\"电子信息工程学院\",\"dormNum\":\"110\",\"grade\":\"2015\",\"isCommunist\":\"0\",\"isDormLeader\":\"1\",\"isStudentLeader\":\"1\",\"major\":\"通信工程\",\"phoneNum\":\"13591991111\",\"status\":\"1\",\"studentId\":\"1\",\"studentName\":\"张三\",\"teachername\":\"张四\"},{\"buildingNum\":\"E1\",\"className\":\"1\",\"classNum\":\"通信15-1班\",\"college\":\"电子信息工程学院\",\"dormNum\":\"110\",\"grade\":\"2015\",\"isCommunist\":\"0\",\"isDormLeader\":\"1\",\"isStudentLeader\":\"1\",\"major\":\"通信工程\",\"phoneNum\":\"13591991111\",\"status\":\"1\",\"studentId\":\"2\",\"studentName\":\"张1\",\"teachername\":\"张四\"},{\"buildingNum\":\"E1\",\"className\":\"1\",\"classNum\":\"通信15-1班\",\"college\":\"电子信息工程学院\",\"dormNum\":\"110\",\"grade\":\"2015\",\"isCommunist\":\"0\",\"isDormLeader\":\"1\",\"isStudentLeader\":\"1\",\"major\":\"通信工程\",\"phoneNum\":\"13591991111\",\"status\":\"1\",\"studentId\":\"3\",\"studentName\":\"张2\",\"teachername\":\"张四\"},{\"buildingNum\":\"E1\",\"className\":\"1\",\"classNum\":\"通信15-1班\",\"college\":\"电子信息工程学院\",\"dormNum\":\"101\",\"grade\":\"2015\",\"isCommunist\":\"0\",\"isDormLeader\":\"1\",\"isStudentLeader\":\"1\",\"major\":\"通信工程\",\"phoneNum\":\"13591991111\",\"status\":\"1\",\"studentId\":\"4\","
+ "\"studentName\":\"张3\",\"teachername\":\"张四\"}]";
//转换成集合
List<Tet> list2=(List<Tet>)JSONArray.toList(JSONArray.fromObject(a), Tet.class);
for (Tet t : list2) {
//取出编号
System.out.println(t.getBuildingNum());
}
//转换成 数组
Tet[] ss =(Tet[])JSONArray.toArray(JSONArray.fromObject(a),Tet.class);
for (Tet t : ss) {
//取出姓名
System.out.println(t.getTeachername());
}
}
}
热心网友
时间:2022-04-23 11:20
可以使用Gson库,先写一个类用于存储每个学生的信息,属性名称和类型都要一一对应,假设这个类的名称为Student,然后通过Student[] students = new Gson().fromJson(jsonString, Student[].class);获得一个存有所有学生实例的数组。不知道能不能这样操作,自己摸索下吧