java 接口套三個Json的實現方式

public class ResResult {

    private int code;

    private String msg;

    private Object re;

    public Object getRe() {
        return re;
    }

    public void setRe(Object re) {
        this.re = re;
    }

    public ResResult() {
        this.code = StatusConst.SUCCESS;
        this.msg = StatusConst.STRSUCCESS;
        this.re = "";
    };

    public ResResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.re = data;
    };

    public ResResult(BindingResult errors) {
        this.code = StatusConst.FAILURE;
        this.msg = StatusConst.ERROR_FORMAT;
        this.re = errors.getAllErrors();
    }

    public ResResult(Object data) {
        this.code = StatusConst.SUCCESS;
        this.msg = StatusConst.STRSUCCESS;
        this.re = data;
    }

    /**
     * @return the code
     */
    public int getCode() {
        return code;
    }

    /**
     * @param code
     *            the code to set
     */
    public void setCode(int code) {
        this.code = code;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg
     *            the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String toString() {
        //JSONObject json = JSONObject.toJSONString(object)(this);
        return JSONObject.toJSONString(this);
    }

}


 "code": 1,
    "msg": "success",
    "re": {
        "YBJ": {
            "red": [
                {
                    "coord": [
                        null,
                        null
                    ],
                    "name": "K1615+423YL-04",
                    "uuid": "2857f01b-cc0f-11e7-982d-00163e32a1d5"
                },
                {
                    "coord": [
                        null,
                        null
                    ],
                    "name": "K1615+431YL-04",
                    "uuid": "2857f4e5-cc0f-11e7-982d-00163e32a1d5"
                },
                {
                    "coord": [
                        null,
                        null
                    ],
                    "name": "K1615+443YL-03",
                    "uuid": "2857f913-cc0f-11e7-982d-00163e32a1d5"
                }
            ],
            "orange": [],
            "green": [
                {
                    "coord": [
                        null,
                        null
                    ],
                    "name": "K1615+390YL-01",
                    "uuid": "2857dbcc-cc0f-11e7-982d-00163e32a1d5"
                },
                {
                    "coord": [
                        null,
                        null
                    ],
                    "name": "K1615+390YL-02",
                    "uuid": "2857df81-cc0f-11e7-982d-00163e32a1d5"
                }
            ]
        }
    }

這種可以用阿里巴巴封裝的Json jar包。

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>

然後代碼裏就可以這樣寫:

	map =measurePointDao.selectMeasurePointInfo(uuid);			
			JSONObject typeJO = new JSONObject();
			typeJO.put("red", new JSONArray());
			typeJO.put("orange", new JSONArray());
			typeJO.put("yellow", new JSONArray());
			typeJO.put("green", new JSONArray());
			typeJO.put("grey", new JSONArray());
			JSONObject jo = new JSONObject();
			for(MeasurePointInfo measurePointInfo:map){
				if(jo.get(measurePointInfo.getCode()) == null||jo.get(measurePointInfo.getCode())==""){
					jo.put(measurePointInfo.getCode(), typeJO);
				}
				String[] a = new String[]{measurePointInfo.getCoordX(),measurePointInfo.getCoordY()};
				JSONObject newjo = new JSONObject();
				newjo.put("name", measurePointInfo.getmName());
				newjo.put("uuid", measurePointInfo.getUuid());
				System.out.println(a);
				newjo.put("coord", a);
				//System.out.println(jo.getJSONObject(measurePointInfo.getCode()).getJSONArray(measurePointInfo.getLevel()));
				jo.getJSONObject(measurePointInfo.getCode()).getJSONArray(measurePointInfo.getLevel()).add(newjo);		
			};
			System.out.println(jo);	
			r.setRe(jo);	


發佈了33 篇原創文章 · 獲贊 37 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章