java:String/Map/JSONObject 互轉
阿新 • • 發佈:2018-11-23
1.json格式字串 轉 JSONObject:
JSONObject jsonobj = JSONObject.parseObject(json格式字串);
2.json格式字串轉map:
Map<String,Map<String,String>> rootMap = JSON.parseObject(oldmesg,Map.class);
3.JSONObject 轉list:
/**
* 將json轉換並拼接為自定義格式的字串 如:1001_positionName_jingyuan
* @param jsonObject
* @return
*/
public static ArrayList jsonTranstoCustomFormat(JSONObject jsonObject) {
StringBuffer stringBuffer = new StringBuffer();
Iterator<String> keys = jsonObject.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
stringBuffer.append(key.toString()).append(",");
}
String[] arr = stringBuffer.toString().split(",");
System.out.println(stringBuffer.toString());
ArrayList<String> arrlis = new ArrayList<String>();
for (int i = 0; i < arr.length; i++) {
String token =arr[i];
String traStr = jsonObject.getString(arr[i]).replace("\"", "").replace("{", "").replace("}", "").replace(":", "_");
String[] arr0 = traStr.split(",");
for (int j = 0; j < arr0.length; j++) {
arrlis.add(token+"_"+arr0[j]);
}
}
return arrlis;
} |