Java 变换地图<;对象、列表<&燃气轮机&燃气轮机;使用gson库创建JSON
我正在尝试使用gson库将Map>转换为JSON。问题是转换为JSON后返回的字符串返回对象的哈希代码,但返回的列表是正确的Java 变换地图<;对象、列表<&燃气轮机&燃气轮机;使用gson库创建JSON,java,Java,我正在尝试使用gson库将Map>转换为JSON。问题是转换为JSON后返回的字符串返回对象的哈希代码,但返回的列表是正确的 {"com.techm.kpn.servicecatalog.common.vo.xsd.Characteristic@61a86b":["C","C++","Java"],"com.techm.kpn.servicecatalog.common.vo.xsd.Characteristic@1780a05":["FreeCharge","Instagram","SnapC
{"com.techm.kpn.servicecatalog.common.vo.xsd.Characteristic@61a86b":["C","C++","Java"],"com.techm.kpn.servicecatalog.common.vo.xsd.Characteristic@1780a05":["FreeCharge","Instagram","SnapChat","Twitter","Whatsapp"]}
在javascript中,我无法打印出Chracteristic对象的属性
这是我申报的地图
Map<Characteristic, List<String>> variantCharacteristics = new HashMap<Characteristic, List<String>>();
当我在上面打印字符串变量时,它正在打印
{"com.techm.kpn.servicecatalog.common.vo.xsd.Characteristic@61a86b":["C","C++","Java"],"com.techm.kpn.servicecatalog.common.vo.xsd.Characteristic@1780a05":["FreeCharge","Instagram","SnapChat","Twitter","Whatsapp"]}
如果希望根据可以使用的默认Gson规则序列化映射键,请尝试使用
enableComplexMapKeySerialization
根据 如果映射键是序列化JSON格式中的复杂类型(即非原语),则启用此功能只会更改序列化形式。映射序列化的默认实现在键上使用
toString()
;但是,当调用此选项时,以下情况之一适用:
映射为JSON对象
在这种情况下,假设注册了一个类型适配器,将某个包含x和y坐标的Point类序列化和反序列化到JSON基元字符串值“(x,y)”。然后,Java映射将被序列化为JsonObject
以下是一个例子:
Gson gson = new GsonBuilder()
.register(Point.class, new MyPointTypeAdapter())
.enableComplexMapKeySerialization()
.create();
Map<Point, String> original = new LinkedHashMap<Point, String>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));
默认情况下,Gson使用其内置类型适配器将应用程序类转换为JSON。如果Gson的默认JSON转换不适合某个类型,则扩展该类以自定义转换。以下是(X,Y)坐标点的类型适配器示例:
public class PointAdapter extends TypeAdapter<Point> {
public Point read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
String xy = reader.nextString();
String[] parts = xy.split(",");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
return new Point(x, y);
}
public void write(JsonWriter writer, Point value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
String xy = value.getX() + "," + value.getY();
writer.value(xy);
}
}
公共类PointAdapter扩展了TypeAdapter{
公共点读取(JsonReader)引发IOException{
if(reader.peek()==JsonToken.NULL){
reader.nextNull();
返回null;
}
字符串xy=reader.nextString();
字符串[]部分=xy.split(“,”);
intx=Integer.parseInt(部分[0]);
int y=Integer.parseInt(部分[1]);
返回新点(x,y);
}
公共void write(JsonWriter writer,Point value)引发IOException{
如果(值==null){
writer.nullValue();
返回;
}
字符串xy=value.getX()+“,”+value.getY();
作者价值(xy);
}
}
阅读更多关于的信息如果您使用
对象
作为密钥,则GSON将使用toString()
方法获取密钥。您希望对象的值是多少?也发布您的java代码。您在Characteristic
类中是否有合适的getter/setter。为Characteristic
类实现toString()方法。您正在使用Spring吗?是的,getter和setter在@Brajwaht类中是正确的,这是新的MyPointTypeAdapter()类。我没有创建我的CharacteristicTypeAdapter。以下是阅读的方法。也更新了帖子。
Gson gson = new GsonBuilder()
.register(Point.class, new MyPointTypeAdapter())
.enableComplexMapKeySerialization()
.create();
Map<Point, String> original = new LinkedHashMap<Point, String>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));
{
"(5,6)": "a",
"(8,8)": "b"
}
public class PointAdapter extends TypeAdapter<Point> {
public Point read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
String xy = reader.nextString();
String[] parts = xy.split(",");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
return new Point(x, y);
}
public void write(JsonWriter writer, Point value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
String xy = value.getX() + "," + value.getY();
writer.value(xy);
}
}