将参数转为json格式字符串的两种方式:
方式一:
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");
map2.put("certType", "IDENTITY_CARD");
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>();
try {
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");
原文地址:http://blog.51cto.com/hebinteng/2150273
时间: 2024-10-29 12:07:09