使用 GSON 反序列化列表

Deserializing list with GSON

我有以下 JSON 对象,我想使用 Google 的 GSON 库反序列化它。不幸的是,我无法正确获取列表。 GSON 找到第一个列表条目但没有找到第二个。

这是我用来调用 GSON 的代码:

Mentions result = gson.fromJson(response, Mentions.class);

这是我的 JSON 文件:

{
    "mentions": [
        {
            "allEntities": [
                {
                    "kbIdentifier": "YAGO:Bob_Dylan",
                    "disambiguationScore": "0.63692"
                }
            ],
            "name": "Dylan",
            "bestEntity": {
                "kbIdentifier": "YAGO:Bob_Dylan",
                "disambiguationScore": "0.63692"
            }
        },
        {
            "name": "Duluth",
            "bestEntity": {
                "kbIdentifier": "YAGO:Duluth\u002c_Minnesota",
                "disambiguationScore": "0.63149"
            }
        }
    ]
}

这些是我创建的普通旧 java 对象:

public class Mentions {
    public List<Mention> mentions = new ArrayList<>();
}

public class Mention {
    @SerializedName("bestEntity")
    public BestEntity entity;
    @SerializedName("name")
    public String name;
}

public class BestEntity {
    @SerializedName("kbIdentifier")
    public String kbIdentifier;
    @SerializedName("disambiguationScore")
    public Double disambiguationScore;
}

我也试过直接反序列化列表,但它只是给我一个错误,说 GSON 期望列表从输入的开头开始。

Type datasetListType = new TypeToken<Collection<Mention>>() {
}.getType();
List<Mention> mentions = gson.fromJson(response, datasetListType);

您不应该使用您创建的 class 吗? I.E 提及

gson.fromJson(response, Mentions.class);

如果我是你,我会映射所有字段以备不时之需,你缺少 allEntities

试试这个 -

AllEntity.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AllEntity {
    @SerializedName("kbIdentifier")
    @Expose
    private String kbIdentifier;
    @SerializedName("disambiguationScore")
    @Expose
    private String disambiguationScore;
    public String getKbIdentifier() {
        return kbIdentifier;
    }
    public void setKbIdentifier(String kbIdentifier) {
        this.kbIdentifier = kbIdentifier;
    }
    public String getDisambiguationScore() {
        return disambiguationScore;
    }
    public void setDisambiguationScore(String disambiguationScore) {
        this.disambiguationScore = disambiguationScore;
    }
    @Override
    public String toString() {
        return "AllEntity [kbIdentifier=" + kbIdentifier
                + ", disambiguationScore=" + disambiguationScore + "]";
    }
}

BestEntity.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class BestEntity {
    @SerializedName("kbIdentifier")
    @Expose
    private String kbIdentifier;
    @SerializedName("disambiguationScore")
    @Expose
    private String disambiguationScore;
    public String getKbIdentifier() {
        return kbIdentifier;
    }
    public void setKbIdentifier(String kbIdentifier) {
        this.kbIdentifier = kbIdentifier;
    }
    public String getDisambiguationScore() {
        return disambiguationScore;
    }
    public void setDisambiguationScore(String disambiguationScore) {
        this.disambiguationScore = disambiguationScore;
    }
    @Override
    public String toString() {
        return "BestEntity [kbIdentifier=" + kbIdentifier
                + ", disambiguationScore=" + disambiguationScore + "]";
    }
}

Mention.java

import java.util.ArrayList;
import java.util.List;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Mention {
    @SerializedName("allEntities")
    @Expose
    private List<AllEntity> allEntities = new ArrayList<AllEntity>();
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("bestEntity")
    @Expose
    private BestEntity bestEntity;
    public List<AllEntity> getAllEntities() {
        return allEntities;
    }
    public void setAllEntities(List<AllEntity> allEntities) {
        this.allEntities = allEntities;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public BestEntity getBestEntity() {
        return bestEntity;
    }
    public void setBestEntity(BestEntity bestEntity) {
        this.bestEntity = bestEntity;
    }
    @Override
    public String toString() {
        return "Mention [allEntities=" + allEntities + ", name=" + name
                + ", bestEntity=" + bestEntity + "]";
    }
}

Main.java

import com.example.ElemntList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {
    private static Gson gson;

    static {
        gson = new GsonBuilder().create();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String s = "{\"mentions\":[{\"allEntities\":[{\"kbIdentifier\":\"YAGO:Bob_Dylan\",\"disambiguationScore\":\"0.63692\"}],\"name\":\"Dylan\",\"bestEntity\":{\"kbIdentifier\":\"YAGO:Bob_Dylan\",\"disambiguationScore\":\"0.63692\"}},{\"name\":\"Duluth\",\"bestEntity\":{\"kbIdentifier\":\"YAGO:Duluth\u002c_Minnesota\",\"disambiguationScore\":\"0.63149\"}}]}";
        ElemntList info = gson.fromJson(s, ElemntList.class);
        System.out.println(info);
   }
}

结果是-

ElemntList [mentions=[Mention [allEntities=[AllEntity [kbIdentifier=YAGO:Bob_Dylan, disambiguationScore=0.63692]], name=Dylan, bestEntity=BestEntity [kbIdentifier=YAGO:Bob_Dylan, disambiguationScore=0.63692]], Mention [allEntities=[], name=Duluth, bestEntity=BestEntity [kbIdentifier=YAGO:Duluth,_Minnesota, disambiguationScore=0.63149]]]]