Cloudant 库在保存文档时添加不必要的属性

Cloudant Library adding unnecessary properties while saving document

这个问题很可能是因为 JSONObject(org.json.JSONObject) 与 cloudant 库不兼容。

有没有其他方法可以使用任何其他对象?

我正在使用以下 cloudant 库,

<dependency>
            <groupId>com.cloudant</groupId>
            <artifactId>cloudant-client</artifactId>
            <version>2.6.2</version>
        </dependency>

这是我的代码

package data.repositories;

import org.json.JSONObject;
import com.cloudant.client.api.*;
import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.model.Response;
import util.Config;

public class DatabaseRepository {

    CloudantClient client = ClientBuilder.account(Config.CLOUDANT_ACCOUNT_NAME)
    .username(Config.CLOUDANT_USER_NAME)
    .password(Config.CLOUDANT_PASSWORD).build();

    public DatabaseRepository() {
JSONObject
    }

    public void Save(String dbName) {
        Database db = client.database("dbTempName", true);
        JSONObject jsonObject = new JSONObject("{hello: data}");
        db.save(jsonObject);
    }
}

保存在 cloudant 数据库中的文档是,

{
  "_id": "1c7f223f74a54e7c9f4c8a713feaa537",
  "_rev": "1-a3cd12379eec936b61f899c8278c9d62",
  "map": {
    "hello": "data"
  }
}

我对 cloudant 不熟悉,但我猜 JsonObject 有一个名为 "map" 的 属性 来保存您的 json 字符串数据(可能有一个 myArray 属性也是),cloudant 将其序列化为 json,从而添加那些 不必要的 值。

我的建议:

1) 尝试像 db.save("{hello: data}") 一样直接保存您的 json 字符串以避免序列化

2) 如果您确实需要创建 JsonObject,请尝试自定义 cloudant 的序列化过程以避免那些额外的字段。

回复评论:

根据我的阅读 here,然后我认为您需要一个 pojo,当序列化为 json 时,它看起来像:

{ 'hello' : 'data' }

类似于:

public class MyClass implements Serializable {
    String hello;

    public MyClass(String hello) {
        this.hello = hello;
    }

    public String getHello() {
        return hello;
    }
}

然后保存为:

db.save(new MyClass("data"));

或者你可以使用 hashmap 代替 pojo:

Map<String, Object> map = new Hashmap ...
map.put("hello", "data");
db.save(map);

查看 README for the repo 中的示例。它表明你想要一个POJO,但你不必实现Serializable。只需创建一个具有 _id_rev 字符串属性的 class。然后根据需要添加 Javascript 对象兼容属性。

// A Java type that can be serialized to JSON
public class ExampleDocument {
  private String _id = "example_id";
  private String _rev = null;
  private boolean isExample;

  public ExampleDocument(boolean isExample) {
    this.isExample = isExample;
  }

  public String toString() {
    return "{ id: " + _id + ",\nrev: " + _rev + ",\nisExample: " + isExample + "\n}";
  }
}

// Create an ExampleDocument and save it in the database
db.save(new ExampleDocument(true));

虽然我还没有尝试过,但 Hashmap 方法也可能有效,如本教程中所讨论的:https://www.ibm.com/blogs/bluemix/2014/07/cloudant_on_bluemix/

// create a simple doc to place into your new database
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("_id", UUID.randomUUID().toString());
doc.put("season", "summer");
doc.put("climate", "arid");
dbc.create(doc);

有问题 似乎 org.json.JSONObject 使用过并且它与 cloudant 客户端库不兼容。我尝试使用 google 对象,它对我很有用。

问题已通过使用 google com.google.gson.JsonObject 而不是 org.json.JSONObject 得到解决。

正确的完整代码如下,

  Database db = client.database("dbTempName", true);

  // Used google.gson.JsonObject instead of org.json.JSONObject. 
  com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
  com.google.gson.JsonObject jsonObject = parser.parse("{\"hello\": \"data\"}").getAsJsonObject();

  db.save(jsonObject);