Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
使用gson-java获取不带密钥的JSON import java.util.List; 导入com.google.gson.annotations.Expose; 公共类TabCompRec{ @公开私有列表单元; 公共列表getCells(){ 返回单元; } 公共空集合单元格(列表单元格){ 这个。细胞=细胞; } } 导入com.google.gson.annotations.Expose; 公共类TabDataCell{ @公开私有字符串值; 公共字符串getCellValue(){ 返回单元格值; } 公共void setCellValue(字符串cellValue){ this.cellValue=cellValue; } } 导入java.util.ArrayList; 导入java.util.List; 导入com.google.gson.gson; 导入com.google.gson.GsonBuilder; 公共类TabMain{ 公共静态void main(字符串[]args){ TabDataCell tdc1=新的TabDataCell(“123”); TabDataCell tdc2=新的TabDataCell(“456”); TabCompRec tcr=新的TabCompRec(); tcr.setCells(Arrays.asList(tdc1,tdc2)); Gson Gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(tcr)); } }_Java_Json_Gson - Fatal编程技术网

使用gson-java获取不带密钥的JSON import java.util.List; 导入com.google.gson.annotations.Expose; 公共类TabCompRec{ @公开私有列表单元; 公共列表getCells(){ 返回单元; } 公共空集合单元格(列表单元格){ 这个。细胞=细胞; } } 导入com.google.gson.annotations.Expose; 公共类TabDataCell{ @公开私有字符串值; 公共字符串getCellValue(){ 返回单元格值; } 公共void setCellValue(字符串cellValue){ this.cellValue=cellValue; } } 导入java.util.ArrayList; 导入java.util.List; 导入com.google.gson.gson; 导入com.google.gson.GsonBuilder; 公共类TabMain{ 公共静态void main(字符串[]args){ TabDataCell tdc1=新的TabDataCell(“123”); TabDataCell tdc2=新的TabDataCell(“456”); TabCompRec tcr=新的TabCompRec(); tcr.setCells(Arrays.asList(tdc1,tdc2)); Gson Gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(tcr)); } }

使用gson-java获取不带密钥的JSON import java.util.List; 导入com.google.gson.annotations.Expose; 公共类TabCompRec{ @公开私有列表单元; 公共列表getCells(){ 返回单元; } 公共空集合单元格(列表单元格){ 这个。细胞=细胞; } } 导入com.google.gson.annotations.Expose; 公共类TabDataCell{ @公开私有字符串值; 公共字符串getCellValue(){ 返回单元格值; } 公共void setCellValue(字符串cellValue){ this.cellValue=cellValue; } } 导入java.util.ArrayList; 导入java.util.List; 导入com.google.gson.gson; 导入com.google.gson.GsonBuilder; 公共类TabMain{ 公共静态void main(字符串[]args){ TabDataCell tdc1=新的TabDataCell(“123”); TabDataCell tdc2=新的TabDataCell(“456”); TabCompRec tcr=新的TabCompRec(); tcr.setCells(Arrays.asList(tdc1,tdc2)); Gson Gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(gson.toJson(tcr)); } },java,json,gson,Java,Json,Gson,以上代码的输出: {“单元”:[ { “cellValue”:“123” }, { “cellValue”:“456” }]} 但是我想要下面这样的json,而不改变域对象结构。我得到了一个变通代码,它迭代上面的json并进行如下转换。但我想知道是否有gson实用程序可以获得如下输出: {“单元”:[ "123", “456”]} 使单元格成为字符串列表,而不是TabDataCell列表 import java.util.List; import com.google.gson.annotati

以上代码的输出:

{“单元”:[ { “cellValue”:“123” }, { “cellValue”:“456” }]}

但是我想要下面这样的json,而不改变域对象结构。我得到了一个变通代码,它迭代上面的json并进行如下转换。但我想知道是否有gson实用程序可以获得如下输出:

{“单元”:[ "123", “456”]}


使单元格成为字符串列表,而不是TabDataCell列表

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));
    }
}

这将为您提供正确的结果

您可以实现自定义serialiser实现
com.google.gson.JsonSerializer
接口:

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));
    }
}

嗨,Jens,谢谢你的回复。由于域对象用于不同的用例,我不能继续更改它。这就是为什么需要一些实用程序来获得预期的json输出。您可以创建新对象并从域对象映射它们
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));
    }
}
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();