Java開発–Map(List)オブジェクトからJSON文字列へ変換する

JAVAコード

1.List–>JSON文字列
@SuppressWarnings(“rawtypes")
public static String simpleListToJsonStr(List<?> list,List<Class> claList) throws IllegalArgumentException, IllegalAccessException{
if(list==null||list.size()==0){
return “[]";
}
String jsonStr = “[“;
for (Object object : list) {
jsonStr += simpleObjectToJsonStr(object,claList)+",";
}
jsonStr = jsonStr.substring(0,jsonStr.length()-1);
jsonStr += “]";
return jsonStr;
}

2.Map–>JSON文字列

public static String simpleMapToJsonStr(Map<?,?> map){
if(map==null||map.isEmpty()){
return “null";
}
String jsonStr = “{“;
Set<?> keySet = map.keySet();
for (Object key : keySet) {
jsonStr += “\""+key+"\":\""+map.get(key)+"\",";
}
jsonStr = jsonStr.substring(0,jsonStr.length()-1);
jsonStr += “}";
return jsonStr;
}

Java

Posted by arkgame