baidu_39234208
baidu_39234208
2018-07-29 07:18
采纳率: 50%
浏览 6.6k

java 嵌套json如何转成list集合

#嵌套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条回答 默认 最新

  • shenanxianshen
    shenanxianshen 2018-07-29 07:41

    转用python import json 直接解决

    点赞 评论
  • qq_30400779
    行人逆旅 2018-07-29 11:36

    使用这个类就可以啊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 2018-07-29 13:15

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

    表3:studentid ceyan yuwen shuxue yingyu

    点赞 评论
  • niaonao
    niaonao 2018-07-30 01:33

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

    String jsonString = "";//你的json 字符串
     ist<User> userList = new ArrayList<>();
     userList = JSON.parseArray(jsonString, User.class) ;
    
    点赞 评论
  • HXNLYW
    葫芦胡 2018-07-30 03:18

    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

    点赞 评论
  • qq_25261763
    qq_25261763 2018-07-30 09:40

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

    点赞 评论
  • qq_25261763
    qq_25261763 2018-07-30 09:41

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

    点赞 评论
  • qq_25261763
    qq_25261763 2018-07-30 09:44

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

    点赞 评论
  • qq_33756493
    昵称_Justin 2018-07-30 09:56

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

    点赞 评论
  • xionglangs
    编程爱好者熊浪 2018-08-02 02:15
    点赞 评论
  • dllsndy
    dllsndy 2018-08-02 08:20

    jackson gson 随便一个json转换包都可以,查下api就行

    点赞 评论

相关推荐