我有類字段Java的地圖<字符串,地圖<字符串,對象>>轉換爲字符串和背部
Map<String, Map<String, Object>> myMap;
我需要實現它ORMlite,我想創建自定義的留存,但不知道將其轉換爲字符串並返回的好方法。
我留存類:
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.field.types.StringType;
import java.sql.SQLException;
import java.util.Map;
public class UserPersister extends StringType {
private static UserPersister INSTANCE;
private UserPersister() {
super(SqlType.STRING, new Class<?>[] {Map.class});
}
public static UserPersister getInstance() {
if (INSTANCE == null)
INSTANCE = new UserPersister();
return INSTANCE;
}
@Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) throws SQLException {
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) javaObject;
return map != null ? getString(map) : null;
}
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
return sqlArg != null ? getFromString((String) sqlArg) : null;
}
private String getString(Map<String, Map<String, Object>> map) {
//implement
}
private Map<String, Map<String, Object>> getFromString(String json) {
//implement
}
的可能的複製[如何HashMap的轉換在Java JSON對象(http://stackoverflow.com/問題/ 12155800/how-to-convert-hashmap-to-json-object-in-java) –