JSONObject JSONArray
1 解析JSONObject为key=value
String params="";//json格式的字符串
JSONObject data = JSONObject .fromObject(params);
Set<Map.Entry<String, Object>> entries = data.entrySet();
Iterator<Map.Entry<String, Object>> it = entries.iterator();
String param="";
while (it.hasNext()) {
Map.Entry<String, Object> next = it.next();
String key = next.getKey();
Object value = next.getValue();
param+=key+"="+value +&"";
}
System.out.println(param);
此时得到的key=value格式的末尾会多余一个"&"
使用如下去除:
str4 = str4.substring(0, str4.length() -1);
即可得到key=value&key=value&key=value
2 java Object ==>jsonObject
Object data1 = ;
JSONObject jsonRandomSMS = JSONObject.parseObject(data1.toString());
3 java Object ==>json格式字符串
Object data1 = ;
String data = JSONObject.toJSONString(data1);
4 java map转json格式字符串
Map<String,Object> map = new HashMap<>();
map.put("channelCode","fghhyttyy123");
map.put("tel","phoneNumber");
map.put("smsTemp","WT_LOGIN_SMS");
map.put("appId","111");
map.put("appIp","127.0.0.1");
String jsonString = JSON.toJSONString(map);// //map转字符串
System.out.println(jsonString);
5 java List 转json格式字符串
List<EnterpriseCertification> userList = new ArrayList<>();
userList.add(e1);
userList.add(e2);
// list转json时保存值为空的属性 SerializerFeature.WriteMapNullValue
String s1 = JSON.toJSONString(userList, SerializerFeature.WriteMapNullValue);
6 json格式的object对象变成map
Map<String, Object> map = JSONObject.parseObject(JSON.toJSONString(whereFields));