• 在后台解析前端传递的json到list中


        花了将近一个多小时来研究这个问题,几近崩溃。

      解决方法:首先创建与你传过来的json数据name字段对应的实体类。

      例[{"name":"1","id","1"},{"name":"2","id","2"}],那么你的实体类的私有变量就是name,id这两个

      我的实体类:

      

    package User;
    
    public class type {
     
        private String one;
        private String two;
        private String three;
        private String four;
        public String getOne() {
            return one;
        }
        public void setOne(String one) {
            this.one = one;
        }
        public String getTwo() {
            return two;
        }
        public void setTwo(String two) {
            this.two = two;
        }
        public String getThree() {
            return three;
        }
        public void setThree(String three) {
            this.three = three;
        }
        public String getFour() {
            return four;
        }
        public void setFour(String four) {
            this.four = four;
        }
        public type(String one, String two, String three, String four) {
            super();
            this.one = one;
            this.two = two;
            this.three = three;
            this.four = four;
        }
        public type() {
            super();
        }
        
        
    }

    然后需要的就是在你的接口方法中所需要的转换(接口方法就是你前端请求的url地址,也可以抽成一个类 ,具体看你):

    //获取传过来的json
               String json=req.getParameter("json");
               //将json字符串转换为json对象
              JSONArray jsonArray=JSONArray.fromObject(json);
              //转换为实体类的list集合
              List<type> list=(List<type>) jsonArray.toCollection(jsonArray, type.class);

      没错,这就已经完成了。你需要那个就调用哪个:例如:list.get(索引).get...(),这个get方法是你在实体类中写的。

      可以输出一下查看成功没有。

      参考链接:https://www.cnblogs.com/austinspark-jessylu/p/7405531.html

  • 相关阅读:
    在线程中更新UI
    Panel容器
    ImageList组件
    PrograssBar控件
    PictureBox控件
    GroupBox控件
    Timer控件Forms.Timer\System.Timers.Timer\System.Threading.Timer
    DataGridView控件1——手动添加数据,遍历数据
    SplitContainer控件
    Git理论知识
  • 原文地址:https://www.cnblogs.com/moxihuishou/p/13908483.html
Copyright © 2020-2023  润新知