为了账号安全,请及时绑定邮箱和手机立即绑定

如何解析Json字符串中的符号“:”

如何解析Json字符串中的符号“:”

30秒到达战场 2022-12-21 15:10:36
我想将 json 字符串解析为 JSONObject 但符号“:”似乎解析错误例如 ->"time": "2019-05-28T16:30:29Z"会出错但是改成"time": "20190526"就OK了这是整个 json 对象:{    "channel": 922875000,    "sf": 12,    "time": "2019-05-28T16:30:29Z",    "gwip": "192.168.0.180",    "gwid": "00001c497b431ff5",    "repeater": "00000000ffffffff",    "systype": 170,    "rssi": -103,    "snr": 20.5,    "snr_max": 33,    "snr_min": 18,    "macAddr": "00000000aabb60ba",    "data": "00000000",    "frameCnt": 8,    "fport": 3}和解析代码:    try {        JSONObject sensorObject = new JSONObject(message.toString());        SensorModel sensorModel = new Gson().fromJson(sensorObject.toString(), SensorModel.class);    } catch (JSONException e) {        logger.error(e.getMessage());    }我怎样才能让他保持相同的"2019:05:26"内容?
查看完整描述

1 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

使用ObjectMapper如下:


import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Date;


public class JSONObject {

    private int channel;

    private int sf;

    private Date time;

    private String gwip;

    private String gwid;

    private String repeater;

    private int systype;

    private int rssi;

    private double snr;

    private double snr_min;

    private double snr_max;

    private String macAddr;

    private String data;

    private int frameCnt;

    private int fport;


    public int getChannel() {

        return channel;

    }


    public void setChannel(int channel) {

        this.channel = channel;

    }


    public int getSf() {

        return sf;

    }


    public void setSf(int sf) {

        this.sf = sf;

    }


    public Date getTime() {

        return time;

    }


    public void setTime(Date time) {

        this.time = time;

    }


    public String getGwip() {

        return gwip;

    }


    public void setGwip(String gwip) {

        this.gwip = gwip;

    }


    public String getGwid() {

        return gwid;

    }


    public void setGwid(String gwid) {

        this.gwid = gwid;

    }


    public String getRepeater() {

        return repeater;

    }


    public void setRepeater(String repeater) {

        this.repeater = repeater;

    }


    public int getSystype() {

        return systype;

    }


    public void setSystype(int systype) {

        this.systype = systype;

    }


    public int getRssi() {

        return rssi;

    }


    public void setRssi(int rssi) {

        this.rssi = rssi;

    }


    public double getSnr() {

        return snr;

    }


    public void setSnr(double snr) {

        this.snr = snr;

    }


    public double getSnr_min() {

        return snr_min;

    }


    public void setSnr_min(double snr_min) {

        this.snr_min = snr_min;

    }


    public double getSnr_max() {

        return snr_max;

    }


    public void setSnr_max(double snr_max) {

        this.snr_max = snr_max;

    }


    public String getMacAddr() {

        return macAddr;

    }


    public void setMacAddr(String macAddr) {

        this.macAddr = macAddr;

    }


    public String getData() {

        return data;

    }


    public void setData(String data) {

        this.data = data;

    }


    public int getFrameCnt() {

        return frameCnt;

    }


    public void setFrameCnt(int frameCnt) {

        this.frameCnt = frameCnt;

    }


    public int getFport() {

        return fport;

    }


    public void setFport(int fport) {

        this.fport = fport;

    }


    @Override

    public String toString() {

        return "JSONObject{" + "channel=" + channel + ", sf=" + sf + ", time=" + time + ", gwip=" + gwip + ", gwid=" + gwid + ", repeater=" + repeater + ", systype=" + systype + ", rssi=" + rssi + ", snr=" + snr + ", snr_min=" + snr_min + ", snr_max=" + snr_max + ", macAddr=" + macAddr + ", data=" + data + ", frameCnt=" + frameCnt + ", fport=" + fport + '}';

    }


    public static void main(String[] args) throws Exception {

        String json = "{\"channel\":922875000,\"sf\":12,\"time\":\"2019-05-28T16:30:29Z\",\"gwip\":\"192.168.0.180\",\"gwid\":\"00001c497b431ff5\",\"repeater\":\"00000000ffffffff\",\"systype\":170,\"rssi\":-103,\"snr\":20.5,\"snr_max\":33,\"snr_min\":18,\"macAddr\":\"00000000aabb60ba\",\"data\":\"00000000\",\"frameCnt\":8,\"fport\":3}";

        ObjectMapper objectMapper = new ObjectMapper();

        JSONObject obj = objectMapper.readValue(json, JSONObject.class);

        System.out.println(obj.toString());

    }

}



查看完整回答
反对 回复 2022-12-21
  • 1 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信