常用方式:
json字符串拼接(目前使用过两种方式):
1.运用StringBuilder拼接 StringBuilder json = new StringBuilder(); json.append("{"); json.append(""uuid":" + """ + uuid + "","); json.append(""phone":" + """ + list.get(i).get("DEST_NUMBER") + "","); json.append(""tempTypeId":"6","); json.append(""tempStatus":"0","); json.append(""account":" + """ + list.get(i).get("DEST_NUMBER") + "","); json.append(""amount":" + """ + Integer.parseInt(list.get(i).get("AMOUNT") + "") / 100 + "","); json.append(""result":"0","); json.append(""source":"JF","); json.append(""first":"","); json.append(""url":"","); json.append(""remark":"""); json.append("}"); String jsonstr = json.toString();2.运用JSONArray(JsonObject)
JSONObject jo = new JSONObject(); // 对象{} JSONArray json = new JSONArray();// 数组[] jo.put("id", Convert.convertNull(hm.get("cost_center_id"))); jo.put("Pid", Convert.convertNull(hm.get("parent_center_id"))); jo.put("name", Convert.convertNull(hm.get("cost_center_name"))); jo.put("isParent", Long.parseLong(Convert.convertNull(hm.get("count"))) > 0 ? true : false); jo.put("levelId",Convert.convertNull(hm.get("level"))); String jsonObj = jo.toString(); json.put(jo); String jsonArr = json.toString();json解析:
1.List idList = (List) JSONUtil.deserialize(agreementIds);2.运用JSONObject
注:
json.append("msg", "a1") json.append("msg", "a2") {"msg":["a1", "a2"]} ---数组追加json.put("msg", "a1")
json.put("msg", "a2") {"msg":"a2"} ---ojbect替换这里一位女程序员的总结很好 :