zoukankan      html  css  js  c++  java
  • json:java中前台向后台传对象数据

    前台传入的是一个json类型的数据,如何在后台解析成想要的数据类型?

    例如:

      

    后台获取了前台一个string类型的数据@RequestParam(value = "forceUpgradeTime") String forceUpgradeTime,
    forceUpgradeTime的格式如下:

    后台转换成我们需要的数据类型:

    import org.json.JSONArray;//导入的是org.json
    public List<Time> convertTime(
    @RequestParam(value = "forceUpgradeTime") String forceUpgradeTime
    ) {
    JSONArray jsonArray = new JSONArray(forceUpgradeTime);
    List<Time> timeList = new ArrayList<>();
    for(int i = 0; i<jsonArray.length(); i++) {
    Time time = new Time();
    time.setStartTime((String) jsonArray.getJSONObject(i).get("startTime"));
    time.setEndTime((String) jsonArray.getJSONObject(i).get("endTime"));
    time.setType((int) jsonArray.getJSONObject(i).get("type"));
    timeList.add(time);
    }
    return timeList;
    }


    定义一个Time model类
    public class Time {
    private String startTime;
    private String endTime;
    private int type;
    public Time(){}
    public Time(String startTime, String endTime, int type) {
    this.startTime = startTime;
    this.endTime = endTime;
    this.type = type;
    }
    public Time(String startTime, String endTime) {
    this.startTime = startTime;
    this.endTime = endTime;
    }

    public String getStartTime() {
    return startTime;
    }

    public void setStartTime(String startTime) {
    this.startTime = startTime;
    }

    public String getEndTime() {
    return endTime;
    }

    public void setEndTime(String endTime) {
    this.endTime = endTime;
    }

    public int getType() {
    return type;
    }

    public void setType(int type) {
    this.type = type;
    }
    }


  • 相关阅读:
    循环图片 yi
    给大家一个经典的.net情感故事 yi
    [东邪西毒][程序员版][原版][剧情] yi
    Sqlite 使用笔记 中文显示为乱码 yi
    sql2005安装过程,(不装C盘) yi
    Visual Studio 2010 美女与程序员的爱情网剧全集 yi
    IT行业几大职业病 yi
    标准化操作
    【ActiveMQ Tuning】Serializing to Disk
    我的山寨敏捷四季之春
  • 原文地址:https://www.cnblogs.com/dakewang/p/6894363.html
Copyright © 2011-2022 走看看