Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson-每个属性都作为字符串_Java_Json_Azure Cosmosdb - Fatal编程技术网

Java Gson-每个属性都作为字符串

Java Gson-每个属性都作为字符串,java,json,azure-cosmosdb,Java,Json,Azure Cosmosdb,我已经通过Azure创建了到DocumentDB的连接,我正在将一个对象保存到数据库中 我的对象是类Car的一个实例 public class Car { private long id; private String name; public Car(long id, String name) { this.id = id; this.name = name; } } 要将对象作为文档存储到DocumentDB,必须从该对象创

我已经通过
Azure
创建了到
DocumentDB
的连接,我正在将一个对象保存到数据库中

我的对象是类
Car
的一个实例

public class Car {
    private long id;
    private String name;

    public Car(long id, String name) {
        this.id = id;
        this.name = name;
    }
}
要将对象作为文档存储到
DocumentDB
,必须从该对象创建一个json字符串

问题在于调用
newgson().toJson(新车(15,“奥迪”))时

它将返回
“{“id”:15,“name”:“Audi”}”
。 但是
DocumentDB
SDK不接受此格式。它抛出JSON异常,因为它需要以下格式:

“{”id:“15”,“name:“Audi”}”

我的问题是
Gson
是否能够用引号“包围”每个属性,以及如何做到这一点


谢谢。

您只需将
id
成员更改为
字符串
类型即可


顺便说一句,查看Gson文档,我看不到任何自动将数字转换为带引号的字符串的选项。

这是正确的,可以将其更改为字符串,也可以将每个基本类型用引号括起来。