使用GSON反序列化列表

时间:2015-10-31 12:27:47

标签: java json 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);

2 个答案:

答案 0 :(得分:0)

你不应该使用你创建的课程吗? I.E提及

# require all standard libraries first
require 'base64'
require 'ostruct'
require 'my_gem/app.rb'  # be sure this is loaded before base!
require 'my_gem/base.rb' # be sure this is loaded before utils!
require 'my_gem/utils.rb' # be sure this is loaded before some other class!

如果我是你,我会映射所有字段以防万一你可能需要它,你缺少 allEntities

答案 1 :(得分:0)

试试这个 -

<强> 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]]]]