【问题标题】:How can add JSON array into Google App datastore如何将 JSON 数组添加到 Google App 数据存储区
【发布时间】:2011-11-22 22:34:49
【问题描述】:

我需要添加一个 JSON 数组来存储到谷歌应用程序数据存储中的解决方案,我认为这在 Python 中是可能的,但我对此并不熟悉,我只需要 Java 代码和 JSON 布局中的简单实现来存储接收到的数据和 Android 加速度计到数据存储区。如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: java json google-app-engine


    【解决方案1】:

    如果您不需要索引数据,只需将 JSON 数据作为文本字符串存储在数据存储中,标记为未索引。如果确实需要对其进行索引,则需要构建一个包含 JSON 数据重要属性的模型,并自行复制值。

    【讨论】:

    • RESTeasy 非常适合将 JSON 映射到 Java 类。 Objectify 非常适合将 Java 类映射到数据存储区。
    • Datastore StringProperty 有 500 个字符的限制。对于较大的文件,TextProperty 最大为 1 MB,您将需要一个 blob。
    【解决方案2】:

    请参阅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()));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2011-02-16
      相关资源
      最近更新 更多