java 嵌套json如何转成list集合 10C

#嵌套json
jsonstr="{
"grade":"一班",
"studentnum":"3",
"student":[{
"xh":"0313001",
"name":"zhangsan",
"chengji": [{"ceyan":"one","yuwen":"71","shuxue":"72","yingyu":"73"},{"ceyan":"two","yuwen":"76","shuxue":"77","yingyu":"78"}],
"xh":"0313002",
"name":"lisi",
"chengji":[{"ceyan":"one","yuwen":"81","shuxue":"82","yingyu":"83"},{"ceyan":"one","yuwen":"86","shuxue":"87","yingyu":"88"}],
"xh":"0313003",
"name":"wangswu",
"chengji":[{"ceyan":"one","yuwen":"91","shuxue":"92","yingyu":"93"},{"ceyan":"one","yuwen":"96","shuxue":"97","yingyu":"98"}]
}]
}"

#定义的JavaBean实体类
public class JsonRootBean {

private String grade;
private String studentnum;
private List<Student> student;
public void setGrade(String grade) {
     this.grade = grade;
 }
 public String getGrade() {
     return grade;
 }

public void setStudentnum(String studentnum) {
     this.studentnum = studentnum;
 }
 public String getStudentnum() {
     return studentnum;
 }

public void setStudent(List<Student> student) {
     this.student = student;
 }
 public List<Student> getStudent() {
     return student;
 }

}

public class Student {

private String xh;
private String name;
private List<Chengji> chengji;
public void setXh(String xh) {
     this.xh = xh;
 }
 public String getXh() {
     return xh;
 }

public void setName(String name) {
     this.name = name;
 }
 public String getName() {
     return name;
 }

public void setChengji(List<Chengji> chengji) {
     this.chengji = chengji;
 }
 public List<Chengji> getChengji() {
     return chengji;
 }

}

public class Chengji {

private String cyss;
private String yuwen;
private String shuxue;
private String yingyu;
public void setCyss(String cyss) {
     this.cyss = cyss;
 }
 public String getCyss() {
     return cyss;
 }

public void setYuwen(String yuwen) {
     this.yuwen = yuwen;
 }
 public String getYuwen() {
     return yuwen;
 }

public void setShuxue(String shuxue) {
     this.shuxue = shuxue;
 }
 public String getShuxue() {
     return shuxue;
 }

public void setYingyu(String yingyu) {
     this.yingyu = yingyu;
 }
 public String getYingyu() {
     return yingyu;
 }

}

#该如何转换啊?

11个回答

使用这个类就可以啊net.sf.json.JSONObject

 @Test
    public void toBean() {
        Chengji c1 = new Chengji();
        c1.setCyss("one");
        c1.setShuxue("72");
        c1.setYingyu("73");
        c1.setYuwen("91");
        Student stu = new Student();
        List<Chengji> list = new ArrayList<>();
        list.add(c1);
        stu.setChengji(list);
        List<Student> students = new ArrayList<>();
        students.add(stu);
        JsonRootBean j1 = new JsonRootBean();
        j1.setGrade("一班");
        j1.setStudent(students);
        j1.setStudentnum("3");
        JSONObject jsonObject = JSONObject.fromObject(j1);
        String str = jsonObject.toString();     
        System.out.println(str);
        String jsonString = "{\"student\":[{\"xh\":\"\",\"name\":\"\",\"chengji\":[{\"yingyu\":\"73\",\"yuwen\":\"91\",\"shuxue\":\"72\",\"cyss\":\"one\"}]}],\"grade\":\"一班\",\"studentnum\":\"3\"}";
        JSONObject jObject = JSONObject.fromObject(jsonString);
        //JSONObject jObject = JSONObject.fromObject(str);
        JsonRootBean jsonRootBean = (JsonRootBean)JSONObject.toBean(jObject,JsonRootBean.class);
        System.out.println(jsonRootBean);
    }

结果:
{"student":[{"xh":"","name":"","chengji":[{"yingyu":"73","yuwen":"91","shuxue":"72","cyss":"one"}]}],"grade":"一班","studentnum":"3"}
JsonRootBean [grade=一班, studentnum=3, student=[net.sf.ezmorph.bean.MorphDynaBean@d2cc05a[
{xh=, name=, chengji=[net.sf.ezmorph.bean.MorphDynaBean@4f933fd1[
{yingyu=73, yuwen=91, shuxue=72, cyss=one}
]]}
]]]

baidu_39234208
baidu_39234208 我是想生成这样的关系表对应的list,方便list直接插入数据库: 表1:gradeid grade studentnum 表2:gradeid xh name studentid 表3:studentid ceyan yuwen shuxue yingyu
大约 2 年之前 回复

转用python import json 直接解决

我是想生成这样的关系表对应的list,方便list直接插入数据库:
表1:gradeid grade studentnum
表2:gradeid xh name studentid

表3:studentid ceyan yuwen shuxue yingyu

引入json jar包,
JSON.parseArray() 方法

String jsonString = "";//你的json 字符串
 ist<User> userList = new ArrayList<>();
 userList = JSON.parseArray(jsonString, User.class) ;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//一句代码搞定
JsonRootBean jsonRootBean = JSON.toJavaObject(JSONObject.parseObject(jsonstr), JsonRootBean .class);

maven引入依赖包:

com.alibaba
fastJson
1.2.17

import com.google.gson.Gson
Gson gson = new Gson();
List<类名> list = gson.fromJson(orgsx,new TypeToken>() {}.getType());

Gson gson = new Gson();
List<类名> list = gson.fromJson(json, new TypeToken>() { }.getType());

尖括号发不了 后面是new TypeToken《List《类名》》(){ }.getType());

直接在你的项目中添加Gson依赖就可以了,Gson最基本的操作都可以解决,https://blog.csdn.net/adventer/article/details/79068498

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐