java後臺JSON操作
阿新 • • 發佈:2018-07-26
trace urn rtt ons try class identity cer ack 將參數轉為json格式字符串的兩種方式:
方式一:
JSONObject jo = new JSONObject();
jo.put("certNo", "522121198710114617");
jo.put("certType", "IDENTITY_CARD");
String jostr = jo.toJSONString(); map2.put("certType", "IDENTITY_CARD");
String jostr2 = JSONObject.toJSONString(map2);
方式一:
JSONObject jo = new JSONObject();
jo.put("certNo", "522121198710114617");
jo.put("certType", "IDENTITY_CARD");
String jostr = jo.toJSONString();
方式二:
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("certNo", "522121198710114617");
String jostr2 = JSONObject.toJSONString(map2);
將json格式字符串轉為Map的方法:
/**
- 將jsonString 解析成 Map<String,Object>>
- @param jsonString
- @return
*/
public static Map<String, Object> getMap(String jsonString) {
Map<String, Object> map = new HashMap<String, Object>();
map = JSON.parseObject(jsonString,Map.class);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
從json對象中拿想要的參數的方法:
Object o = JSON.toJSON(response);
String str = o.toString();
JSONObject jo = JSONObject.parseObject(str);
JSONObject params = jo.getJSONObject("params");
java後臺JSON操作