使用 gson java 在没有密钥的情况下获取 JSON
Get JSON without key using gson java
import java.util.List;
import com.google.gson.annotations.Expose;
public class TabCompRec {
@Expose private List<TabDataCell> cells;
public List<TabDataCell> getCells() {
return cells;
}
public void setCells(List<TabDataCell> cells) {
this.cells = cells;
}
}
import com.google.gson.annotations.Expose;
public class TabDataCell {
@Expose private String cellValue;
public String getCellValue() {
return cellValue;
}
public void setCellValue(String cellValue) {
this.cellValue = cellValue;
}
}
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class TabMain {
public static void main(String[] args) {
TabDataCell tdc1 = new TabDataCell("123");
TabDataCell tdc2 = new TabDataCell("456");
TabCompRec tcr = new TabCompRec();
tcr.setCells(Arrays.asList(tdc1, tdc2));
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println(gson.toJson(tcr));
}
}
以上代码的输出:
{ "cells": [
{
"cellValue": "123"
},
{
"cellValue": "456"
} ] }
但我想要 json 如下所示,而不更改域对象结构。我得到了一个解决方法代码,它迭代上面的 json 并按如下方式转换。但我想知道任何可用的 gson 实用程序来获得这样的输出:
{ "cells": [
"123",
"456" ] }
使单元格成为字符串列表而不是 TabDataCell 列表
import java.util.List;
import com.google.gson.annotations.Expose;
public class TabCompRec {
@Expose private List<String> cells;
public List<String> getCells() {
return cells;
}
public void setCells(List<String> cells) {
this.cells = cells;
}
}
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class TabMain {
public static void main(String[] args) {
TabCompRec tcr = new TabCompRec();
tcr.setCells(Arrays.asList("123", "456"));
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println(gson.toJson(tcr));
}
}
这应该会给你正确的结果
您可以实现实现 com.google.gson.JsonSerializer
接口的自定义序列化程序:
class TabDataCellJsonSerializer implements JsonSerializer<TabDataCell> {
@Override
public JsonElement serialize(TabDataCell src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getCellValue());
}
}
您可以像下面这样注册:
Gson gson = new GsonBuilder()
.registerTypeAdapter(TabDataCell.class, new TabDataCellJsonSerializer())
.create();
import java.util.List;
import com.google.gson.annotations.Expose;
public class TabCompRec {
@Expose private List<TabDataCell> cells;
public List<TabDataCell> getCells() {
return cells;
}
public void setCells(List<TabDataCell> cells) {
this.cells = cells;
}
}
import com.google.gson.annotations.Expose;
public class TabDataCell {
@Expose private String cellValue;
public String getCellValue() {
return cellValue;
}
public void setCellValue(String cellValue) {
this.cellValue = cellValue;
}
}
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class TabMain {
public static void main(String[] args) {
TabDataCell tdc1 = new TabDataCell("123");
TabDataCell tdc2 = new TabDataCell("456");
TabCompRec tcr = new TabCompRec();
tcr.setCells(Arrays.asList(tdc1, tdc2));
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println(gson.toJson(tcr));
}
}
以上代码的输出:
{ "cells": [ { "cellValue": "123" }, { "cellValue": "456" } ] }
但我想要 json 如下所示,而不更改域对象结构。我得到了一个解决方法代码,它迭代上面的 json 并按如下方式转换。但我想知道任何可用的 gson 实用程序来获得这样的输出:
{ "cells": [ "123", "456" ] }
使单元格成为字符串列表而不是 TabDataCell 列表
import java.util.List;
import com.google.gson.annotations.Expose;
public class TabCompRec {
@Expose private List<String> cells;
public List<String> getCells() {
return cells;
}
public void setCells(List<String> cells) {
this.cells = cells;
}
}
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class TabMain {
public static void main(String[] args) {
TabCompRec tcr = new TabCompRec();
tcr.setCells(Arrays.asList("123", "456"));
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println(gson.toJson(tcr));
}
}
这应该会给你正确的结果
您可以实现实现 com.google.gson.JsonSerializer
接口的自定义序列化程序:
class TabDataCellJsonSerializer implements JsonSerializer<TabDataCell> {
@Override
public JsonElement serialize(TabDataCell src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getCellValue());
}
}
您可以像下面这样注册:
Gson gson = new GsonBuilder()
.registerTypeAdapter(TabDataCell.class, new TabDataCellJsonSerializer())
.create();