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

Java Cloudant库在保存文档时添加不必要的属性,java,json,cloudant,Java,Json,Cloudant,这个问题很可能是因为JSONObject(org.json.JSONObject)与cloudant库不兼容 是否有其他方法可以使用任何其他对象 我正在使用下面的cloudant库 <dependency> <groupId>com.cloudant</groupId> <artifactId>cloudant-client</artifactId> <ver

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

是否有其他方法可以使用任何其他对象

我正在使用下面的cloudant库

<dependency>
            <groupId>com.cloudant</groupId>
            <artifactId>cloudant-client</artifactId>
            <version>2.6.2</version>
        </dependency>
保存在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的序列化过程,以避免额外的字段

针对评论:

从我所读到的内容来看,我认为您需要一个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(新的MyClass(“数据”))

或者,您可以使用hashmap而不是pojo:

Map<String, Object> map = new Hashmap ...
map.put("hello", "data");
db.save(map);
Map Map=newhashmap。。。
map.put(“你好”,“数据”);
db.save(map);

我不熟悉cloudant,但我猜JsonObject有一个名为“map”的属性,用于保存json字符串数据(可能还有一个myArray属性),cloudant将其序列化为json,从而添加那些不必要的值

我的建议是:

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

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

针对评论:

从我所读到的内容来看,我认为您需要一个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(新的MyClass(“数据”))

或者,您可以使用hashmap而不是pojo:

Map<String, Object> map = new Hashmap ...
map.put("hello", "data");
db.save(map);
Map Map=newhashmap。。。
map.put(“你好”,“数据”);
db.save(map);

查看中的示例。它表明您需要一个POJO,但不必实现可序列化的
。只需创建一个具有
\u id
\u rev
字符串属性的类。然后根据需要添加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方法也可以工作,正如本教程中所讨论的:

//创建一个简单的文档以放入新数据库中
Map doc=new HashMap();
doc.put(“_id”,UUID.randomUUID().toString());
put文件(“季节”、“夏季”);
put文件(“气候”、“干旱”);
dbc.create(doc);

查看中的示例。它表明您需要一个POJO,但不必实现可序列化的
。只需创建一个具有
\u id
\u rev
字符串属性的类。然后根据需要添加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方法也可以工作,正如本教程中所讨论的:

//创建一个简单的文档以放入新数据库中
Map doc=new HashMap();
doc.put(“_id”,UUID.randomUUID().toString());
put文件(“季节”、“夏季”);
put文件(“气候”、“干旱”);
dbc.create(doc);

有疑问的是,似乎使用了org.json.JSONObject,它与cloudant客户端库不兼容。我试过使用谷歌对象,它对我很好

通过使用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);

有疑问的是,似乎使用了org.json.JSONObject,它与cloudant客户端库不兼容。我试过使用谷歌对象,它对我很好

通过使用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);

db.save(“{hello:data}”)无法工作,因为save方法类似于save(Object-Object),具有Object参数的方法不会接受字符串吗?(字符串是对象)编译时无错误。。。但运行时显示errordb.save(“{hello:data}”)无法工作,因为save方法类似于save(Object-Object),带有Object参数的方法不接受字符串吗?(字符串是对象)编译时无错误。。。但运行时显示错误