Java 如何使用自动值gson映射未知json字段

Java 如何使用自动值gson映射未知json字段,java,android,gson,retrofit,auto-value,Java,Android,Gson,Retrofit,Auto Value,我正在android上制作一个简单的github gist view应用程序,它可以与用户对话 但我不确定如何使用AutoValue和GSON映射这个json响应。请注意,我已经删除了很多钥匙,因为我目前不需要它,实际的 反应看起来像 示例响应 问题 我已经读到,我可以在字符串和文件对象之间映射以使其工作。但它不起作用,因为文件键是或可以是对象的对象 那么我如何用自动值gson映射这些未知对象的对象呢?有可能吗 代码 GistResponse.java @AutoValue 公共抽象类GistR

我正在android上制作一个简单的github gist view应用程序,它可以与用户对话

但我不确定如何使用AutoValue和GSON映射这个json响应。请注意,我已经删除了很多钥匙,因为我目前不需要它,实际的 反应看起来像

示例响应 问题 我已经读到,我可以在
字符串
文件
对象之间映射以使其工作。但它不起作用,因为
文件
键是或可以是对象的对象

那么我如何用自动值gson映射这些未知对象的对象呢?有可能吗

代码 GistResponse.java
@AutoValue
公共抽象类GistResponse{
@序列化名称(“id”)
公共抽象字符串getId();
@序列化名称(“文件”)
公共抽象文件名getFileNameList();
公共静态生成器(){
返回新的AutoValue_GistResponse.Builder();
}
@AutoValue.Builder
公共抽象静态类生成器{
公共抽象生成器setId(字符串值);
公共摘要生成器setFileNameList(文件名值);
公共抽象GistResponse build();
}
公共静态类型适配器类型适配器(Gson Gson){
返回新的AutoValue\u GistResponse.GsonTypeAdapter(gson);
}
}
FileName.java
@AutoValue
公共抽象类文件名{
公共抽象映射getFileList();
公共静态生成器(){
返回新的AutoValue_FileName.Builder();
}
@AutoValue.Builder
公共抽象静态类生成器{
公共抽象生成器setFilesList(映射值);
公共抽象文件名build();
}
公共静态类型适配器类型适配器(Gson Gson){
返回新的AutoValue_FileName.GsonTypeAdapter(gson);
}
}
File.java
@AutoValue
公共抽象类文件{
@序列化名称(“文件名”)
公共抽象字符串getFileName();
@序列化名称(“类型”)
公共抽象字符串getType();
@序列化名称(“语言”)
公共抽象字符串getLanguage();
@SerializedName(“原始url”)
公共抽象字符串getRawUrl();
@序列化名称(“大小”)
公共抽象整数getSize();
公共静态生成器(){返回新的AutoValue_File.Builder();}
@AutoValue.Builder
公共抽象静态类生成器{
公共抽象生成器setFileName(字符串值);
公共抽象生成器setType(字符串值);
公共抽象生成器语言(字符串值);
公共抽象生成器setRawUrl(字符串值);
公共抽象生成器setSize(整数值);
公共抽象文件构建();
}
公共静态类型适配器类型适配器(Gson Gson){
返回新的AutoValue_文件.GsonTypeAdapter(gson);
}
}
相关主题和信息:

我找到了一个解决方案,它包括编写一个定制的
TypeAdapter
,实际上非常简单

代码如下:

public class FileTypeAdapter extends TypeAdapter {

    @Override
    public void write(JsonWriter out, Object value) throws IOException {}

    @Override
    public List<File> read(JsonReader jsonReader) throws IOException {

        ArrayList<File> fileList = new ArrayList<>();

        if (jsonReader.peek() == JsonToken.NULL) {
            jsonReader.nextNull();
            return fileList;
        }

        jsonReader.beginObject();
        while (jsonReader.hasNext()) {

            jsonReader.nextName();

            if (jsonReader.peek() == JsonToken.NULL) {
                jsonReader.nextNull();
                continue;
            }

            File.Builder file = File.builder();

            jsonReader.beginObject();
            while (jsonReader.hasNext()) {

                String nextName = jsonReader.nextName();

                switch (nextName) {
                    case "filename": {
                        String nextString = jsonReader.nextString();
                        file.setFileName(nextString);
                        break;
                    }
                    case "type": {
                        String nextString = jsonReader.nextString();
                        file.setType(nextString);
                        break;
                    }
                    case "language": {

                        if (jsonReader.peek() == JsonToken.NULL) {
                            jsonReader.nextNull();
                        } else {
                            String nextString = jsonReader.nextString();
                            file.setLanguage(nextString);
                        }
                        break;
                    }
                    case "raw_url": {
                        String nextString = jsonReader.nextString();
                        file.setRawUrl(nextString);
                        break;
                    }
                    case "size": {
                        Integer nextInt = jsonReader.nextInt();
                        file.setSize(nextInt);
                        break;
                    }
                    default: {
                        jsonReader.skipValue();
                    }
                }
            }
            fileList.add(file.build());
            jsonReader.endObject();
        }
        jsonReader.endObject();
        return fileList;
    }
}
现在可以解析它了

@AutoValue
public abstract class GistResponse {

    @SerializedName("id")
    public abstract String getId();

    @SerializedName("files")
    public abstract FileName getFileNameList();

    public static Builder builder() {
        return new AutoValue_GistResponse.Builder();
    }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setId(String value);
        public abstract Builder setFileNameList(FileName value);

        public abstract GistResponse build();
    }

    public static TypeAdapter<GistResponse> typeAdapter(Gson gson) {
        return new AutoValue_GistResponse.GsonTypeAdapter(gson);
    }

}
@AutoValue
public abstract class FileName {

    public abstract Map<String, File> getFilesList();

    public static Builder builder() {
        return new AutoValue_FileName.Builder();
    }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setFilesList(Map<String, File> value);
        public abstract FileName build();
    }

    public static TypeAdapter<FileName> typeAdapter(Gson gson) {
        return new AutoValue_FileName.GsonTypeAdapter(gson);
    }
}
@AutoValue
public abstract class File {

    @SerializedName("filename")
    public abstract String getFileName();

    @SerializedName("type")
    public abstract String getType();

    @SerializedName("language")
    public abstract String getLanguage();

    @SerializedName("raw_url")
    public abstract String getRawUrl();

    @SerializedName("size")
    public abstract Integer getSize();

    public static Builder builder() { return new AutoValue_File.Builder(); }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setFileName(String value);
        public abstract Builder setType(String value);
        public abstract Builder setLanguage(String value);
        public abstract Builder setRawUrl(String value);
        public abstract Builder setSize(Integer value);

        public abstract File build();
    }

    public static TypeAdapter<File> typeAdapter(Gson gson) {
        return new AutoValue_File.GsonTypeAdapter(gson);
    }
}
public class FileTypeAdapter extends TypeAdapter {

    @Override
    public void write(JsonWriter out, Object value) throws IOException {}

    @Override
    public List<File> read(JsonReader jsonReader) throws IOException {

        ArrayList<File> fileList = new ArrayList<>();

        if (jsonReader.peek() == JsonToken.NULL) {
            jsonReader.nextNull();
            return fileList;
        }

        jsonReader.beginObject();
        while (jsonReader.hasNext()) {

            jsonReader.nextName();

            if (jsonReader.peek() == JsonToken.NULL) {
                jsonReader.nextNull();
                continue;
            }

            File.Builder file = File.builder();

            jsonReader.beginObject();
            while (jsonReader.hasNext()) {

                String nextName = jsonReader.nextName();

                switch (nextName) {
                    case "filename": {
                        String nextString = jsonReader.nextString();
                        file.setFileName(nextString);
                        break;
                    }
                    case "type": {
                        String nextString = jsonReader.nextString();
                        file.setType(nextString);
                        break;
                    }
                    case "language": {

                        if (jsonReader.peek() == JsonToken.NULL) {
                            jsonReader.nextNull();
                        } else {
                            String nextString = jsonReader.nextString();
                            file.setLanguage(nextString);
                        }
                        break;
                    }
                    case "raw_url": {
                        String nextString = jsonReader.nextString();
                        file.setRawUrl(nextString);
                        break;
                    }
                    case "size": {
                        Integer nextInt = jsonReader.nextInt();
                        file.setSize(nextInt);
                        break;
                    }
                    default: {
                        jsonReader.skipValue();
                    }
                }
            }
            fileList.add(file.build());
            jsonReader.endObject();
        }
        jsonReader.endObject();
        return fileList;
    }
}
@AutoValue
public abstract class GistResponse {

    @SerializedName("id")
    public abstract String getId();

    @GsonTypeAdapter(FileTypeAdapter.class)
    @SerializedName("files")
    public abstract FileName getFileNameList();

    public static Builder builder() {
        return new AutoValue_GistResponse.Builder();
    }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setId(String value);
        public abstract Builder setFileNameList(FileName value);

        public abstract GistResponse build();
    }

    public static TypeAdapter<GistResponse> typeAdapter(Gson gson) {
        return new AutoValue_GistResponse.GsonTypeAdapter(gson);
    }

}