问题描述
我有一个类:
@Table(name = "Control")
public class Control extends Model{
@Column
private String name;
[...]
@Column
private Map<String, Object> properties;
}
我在这个类中有其他字段,只有那些足以显示什么是我的问题。
I have other fields in this class, but i wrote here only those which is enough to show what is my problem.
我有一个关于插入的例外:
I got an exception about insertion:
09-14 22:11:34.542: E/SQLiteLog(11482): (1) table Control has no column named properties
Object可以是String []或String。
Object can be either String[] or String.
如果我没有在HashMap数据上放置@Column,那么一切都是罚款只有他们不存储的问题。
If I dont place @Column on HashMap data, than everything works "fine" only the problem they are not stored.
所以我的问题是我应该如何解决存储这个hashmap。
So my question is how should I solve storing this hashmap.
我的数据来自API,它始终不具有相同的属性:
My data comes from API, and it has not the same properties always:
controls: [
{
bundle: "CloseButton",
frame: "0,0,25,25"
},
{
referenceKey: "AccelerationGyro",
name: "Acceleration Sensor",
bundle: "Gyro",
properties: {
axis: "Z"
observedKey: "brakeState",
requiedValue: "1"
},
channels: {
CH: "Acceleration"
}
},
{
referenceKey: "SteeringGyro",
name: "Steering Sensor",
bundle: "Gyro",
properties: {
axis: "Y"
allowedOnI: [
"@Lights",
"@Rack",
"@ReversingLamp"
],
},
channels: {
CH: "Steering"
}
},
我想使用ActiveAndroid lib存储这个类。但不幸的是,我找不到任何解决方案,这在网上。
I want to store this class with using ActiveAndroid lib. But unfortunatelly I could not find any solution to this on the web.
我不知道如果我的问题很清楚,我可以提供额外的东西,如果需要。
我只想存储HashMap。 :P可能是以某种方式连接到TypeSerializers?
I'm not sure if my question is clear, I can provide additional stuff if needed.I just want to store the HashMap too. :P Is it maybe connected somehow to TypeSerializers?
感谢您的回答提前。
推荐答案
ActiveAndroid不会持久映射
默认类型。
ActiveAndroid don't persist Map
type by default.
您必须自行撰写,才能序列化这个映射
为某种格式的字符串(例如:JSON),反序列化为你的 Map
。
Yes, you are right, you must write your own TypeSerializer
, to serialize this Map
as an String in some format (e.g: JSON) and deserialize the String to your Map
.
也许这段代码可以帮助你开始:
Maybe this code can help you to start this:
final public class UtilMapSerializer extends TypeSerializer {
@Override
public Class<?> getDeserializedType() {
return Map.class;
}
@Override
public Class<?> getSerializedType() {
return String.class;
}
@Override
public String serialize(Object data) {
if (data == null) {
return null;
}
// Transform a Map<String, Object> to JSON and then to String
return new JSONObject((Map<String, Object>) data).toString();
}
@Override
public Map<String, Object> deserialize(Object data) {
if (data == null) {
return null;
}
// Properties of Model
Map<String, Object> map = new HashMap<String, Object>();
try {
JSONObject json = new JSONObject((String) data);
for(Iterator it = json.keys(); it.hasNext();) {
String key = (String) it.next();
map.put(key, json.get(key));
}
} catch (JSONException e) {
e.printStackTrace();
}
return map;
}
}
然后注册为 ;
And register then as
<meta-data>
in your <Application>
.
<meta-data android:name="AA_SERIALIZERS" android:value="my.package.UtilMapSerializer" />
详情:
这篇关于存储HashMap< String,Object>与使用ActiveAndroid库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!