2015-10-31 195 views
0

我有以下JSON對象,我想使用Google的GSON庫反序列化它。不幸的是,我無法正確地獲取列表。 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希望列表啓動一個t輸入的開始。

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

你可以發佈你與你想要的結果,結果呢? – Tunaki

+0

難道你不會錯過'allEntities'的一些映射嗎? –

+0

所有實體均不包含相關信息。真正的JSON文件包含更多不相關的信息,我只是忘記從最小的例子中刪除AllEntities。 當然,如果我稍後決定需要AllEntities,我會添加一個映射。 – icehawk

回答

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

您不應該使用您創建的類嗎?即提及

gson.fromJson(response, Mentions.class); 

如果我是你,我會映射所有領域,以防萬一你需要它,你就錯過allEntities