利用fastjson解析即信语音json

来源:互联网 发布:织梦 编辑器字体修改 编辑:程序博客网 时间:2022/08/11 17:36

即信的语音状态报告JSON示例:
{
“type”:”VOICE”,
“total”:”2”,
“reportList”: [
{
“uuid”: “d8f4a192-113c-4e5a-9173-b18a4881eb07”,
“phone”: “135XXXXXXXXX”,
“extend”: “”,
“reportTime”: “20161115123020”,
“result”: “0”,
“durationTime”: “60”
},
{
“uuid”: “d8f4a192-113c-4e5a-9173-b18a4881eb07”,
“phone”: “135XXXXXXXXX”,
“extend”: “”,
“reportTime”: “20161115123020”,
“result”: “0”,
“durationTime”: “60”
}
]
}
这里利用fastjson解析该json:

@Controller@RequestMapping(value = "/自定义")public class NoticeNotifyController{    private static Logger logger = LoggerFactory.getLogger(NoticeNotifyController.class);    @RequestMapping(value = "自定义", method = RequestMethod.POST)    public void receiveNotify(HttpServletRequest request, HttpServletResponse response) throws ParseException {        try {            StringBuffer sb = new StringBuffer() ;             InputStream is = request.getInputStream();             InputStreamReader isr = new InputStreamReader(is);               BufferedReader br = new BufferedReader(isr);             String s = "" ;             while((s=br.readLine())!=null){             sb.append(s) ;             }             String str =sb.toString();//json数据流             if(str.length()>0){                PageData pd = new PageData();                UUsa u1 = JSON.parseObject(str,UUsa.class);                 Usa[] reportList = u1.getReportList();                int total = u1.getTotal();                if(reportList.length>0){                    for(int i=0;i<reportList.length;i++){                        Usa uu= new Usa();                        uu = reportList[i];                        String called = uu.getDest();                        String uuid = uu.getUuid();                        int result = uu.getResult();                        String reportTime = uu.getReportTime();                        int durationTime = uu.getDurationTime();                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");                        Date dd = simpleDateFormat.parse(reportTime);                        long ts = dd.getTime();//转为时间戳                    }                }            }        } catch (Exception e1) {            e1.printStackTrace();        }    }   }class Usa {    @Override    public String toString() {        return "Usa [dest=" + dest + ", extend=" + extend + ", reportTime="                + reportTime + ", result=" + result + ", durationTime="                + durationTime + ", uuid=" + uuid + "]";    }    public String getUuid() {        return uuid;    }    public void setUuid(String uuid) {        this.uuid = uuid;    }    private String dest;//被呼叫号码     public String getDest() {        return dest;    }    public void setDest(String dest) {        this.dest = dest;    }    public String getExtend() {        return extend;    }    public void setExtend(String extend) {        this.extend = extend;    }    public String getReportTime() {        return reportTime;    }    public void setReportTime(String reportTime) {        this.reportTime = reportTime;    }    public Integer getResult() {        return result;    }    public void setResult(Integer result) {        this.result = result;    }    public Integer getDurationTime() {        return durationTime;    }    public void setDurationTime(Integer durationTime) {        this.durationTime = durationTime;    }    private String extend;//用户自定义拓展回传参数,可为null,extend 目前没有使用,可以忽略它    private String reportTime;//状态报告时间,格式为yyyyMMddHHmmss    private Integer result;//消息状态,请查看对应全局错误码    private Integer durationTime;//通话持续时间,单位:秒    private String uuid;//消息唯一ID,与下发时返回的uuid一致} class UUsa {    @Override    public String toString() {        return "UUsa [type=" + type + ", total=" + total + ", reportList="                + Arrays.toString(reportList) + "]";    }    private String type;//‘VOICE’    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public Integer getTotal() {        return total;    }    public void setTotal(Integer total) {        this.total = total;    }    public Usa[] getReportList() {        return reportList;    }    public void setReportList(Usa[] reportList) {        this.reportList = reportList;    }    private Integer total;//当前数据包含有回调状态报告条数,即信并不是发送一条语音通知就回调的,状态报告回来后会回调,如果有多条会合并,节省系统资源 ,并不是一次一个的调用    private Usa[] reportList;//数组格式,状态报告列表}
0 0