【发布时间】:2011-11-22 22:34:49
【问题描述】:
我需要添加一个 JSON 数组来存储到谷歌应用程序数据存储中的解决方案,我认为这在 Python 中是可能的,但我对此并不熟悉,我只需要 Java 代码和 JSON 布局中的简单实现来存储接收到的数据和 Android 加速度计到数据存储区。如果有人可以帮助我,那就太好了。
【问题讨论】:
标签: java json google-app-engine
我需要添加一个 JSON 数组来存储到谷歌应用程序数据存储中的解决方案,我认为这在 Python 中是可能的,但我对此并不熟悉,我只需要 Java 代码和 JSON 布局中的简单实现来存储接收到的数据和 Android 加速度计到数据存储区。如果有人可以帮助我,那就太好了。
【问题讨论】:
标签: java json google-app-engine
如果您不需要索引数据,只需将 JSON 数据作为文本字符串存储在数据存储中,标记为未索引。如果确实需要对其进行索引,则需要构建一个包含 JSON 数据重要属性的模型,并自行复制值。
【讨论】:
请参阅here 了解 JSON 实体映射的实现。
/**
* Sets the properties of the specified entity by the specified json object.
*
* @param entity the specified entity
* @param jsonObject the specified json object
* @throws JSONException json exception
*/
public static void setProperties(final Entity entity,
final JSONObject jsonObject)
throws JSONException {
@SuppressWarnings("unchecked")
final Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
final String key = keys.next();
final Object value = jsonObject.get(key);
if (!GAE_SUPPORTED_TYPES.contains(value.getClass())
&& !(value instanceof Blob)) {
throw new RuntimeException("Unsupported type[class=" + value.
getClass().getName() + "] in Latke GAE repository");
}
if (value instanceof String) {
final String valueString = (String) value;
if (valueString.length()
> DataTypeUtils.MAX_STRING_PROPERTY_LENGTH) {
final Text text = new Text(valueString);
entity.setProperty(key, text);
} else {
entity.setProperty(key, value);
}
} else if (value instanceof Number
|| value instanceof Date
|| value instanceof Boolean
|| GAE_SUPPORTED_TYPES.contains(value.getClass())) {
entity.setProperty(key, value);
} else if (value instanceof Blob) {
final Blob blob = (Blob) value;
entity.setProperty(key,
new com.google.appengine.api.datastore.Blob(
blob.getBytes()));
}
}
}
【讨论】: