zoukankan      html  css  js  c++  java
  • fastjson解析list ,object中含有list, object中含有map

    1.首先定义测试vo

    package com.haiyisoft.cAssistantWeb.ui;

    import java.sql.Timestamp;

    public class vo {
    private Long id;
    private Timestamp date;
    public Timestamp getDate() {
    return date;
    }
    public void setDate(Timestamp date) {
    this.date = date;
    }
    private Long craeateopear;
    public Long getId() {
    return id;
    }
    public void setId(Long id) {
    this.id = id;
    }


    public Long getCraeateopear() {
    return craeateopear;
    }
    public void setCraeateopear(Long craeateopear) {
    this.craeateopear = craeateopear;
    }

    }

    2.进行测试

    package com.haiyisoft.cAssistantWeb.ui;

    import java.sql.Date;
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import com.alibaba.fastjson.JSON;
    import com.haiyisoft.cAssistantWeb.vo.ReturnValueVo;
    import com.haiyisoft.cloud.web.util.CommonUtil;

    public class test {
    public static void main(String[] args) throws Exception {
    Map<String, List<vo>> map = new HashMap<String, List<vo>>();
    System.out.println("获取当前时间:"+CommonUtil.getDate());
    List<vo> list =new ArrayList<vo>();
    for(int i=0;i<4;i++){
    vo vo =new vo();
    vo.setId(Long.valueOf("1"+i));
    vo.setCraeateopear(Long.valueOf("2"+i));
    vo.setDate(CommonUtil.getDate());
    list.add(vo);
    }
    map.put("listmap", list);
    ReturnValueVo result = JsonReturn.assemblyBean(list, StateEnum.SUCCESS.getStatus(),"成功");
    ReturnValueVo resultmap = JsonReturn.assemblyBean(map, StateEnum.SUCCESS.getStatus(),"成功");
    String JSONObject = JSON.toJSONString(result);//将object打成json串
    System.out.println(JSONObject);
    String JSONObjectList = JSON.toJSONString(list);//将list打成json串
    String JSONObjectMap = JSON.toJSONString(resultmap);//将map打成json串
    //=============================================================================解析object
    ReturnValueVo parseObject = JSON.parseObject(JSONObject, ReturnValueVo.class);
    Object list2= parseObject.getObj();//获取object内容
    String list2JSONObject = JSON.toJSONString(list2);//将object内容转成json串
    List<vo> list4= JSON.parseArray(list2JSONObject , vo.class);//再对list解析
    System.out.println(list4.get(0).getDate());//获取时间
    //===========================================解析list
    List<vo> list3= JSON.parseArray(JSONObjectList, vo.class);
    Timestamp id = list3.get(0).getDate();
    System.out.println(id);
    //===========================================解析map
    ReturnValueVo parseMap= JSON.parseObject(JSONObjectMap, ReturnValueVo.class);
    Object map2= parseMap.getObj();//获取object内容
    String JSONMap2 = JSON.toJSONString(map2);//将map打成json串
    Map maps = (Map)JSON.parse(JSONMap2);
    Object maplist = maps.get("listmap");
    String JSONlist2 = JSON.toJSONString(maplist);//将map打成json串
    List<vo> listc= JSON.parseArray(JSONlist2 , vo.class);//再对list解析
    Timestamp cc = listc.get(0).getDate();
    System.out.println(cc);

    }
    }

  • 相关阅读:
    从RUU中提取HTC官方ROM
    AndroidRom制作(一)——Rom结构介绍、精简和内置、一般刷机过程
    写在"跳槽旺季"
    从团宝危机谈行业洗牌
    Session和Cookie的关系
    Django常用模板标签
    ASP.NET论坛调查
    SourceForge支持新的版本控制系统
    ORACLE传奇
    Linked Data下一代WWW
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/9650373.html
Copyright © 2011-2022 走看看