如何让GSON
库正确地将以下JSON
字符串转换为对象。我已经尝试了很长时间,但它似乎只是选择了2" Word"对象并将成员字段留空或为空。
JSON:
{
"words": [
{
"Word": {
"id": "1",
"word": "submarine",
"word_syllables": "sub-mar-ine",
"picture": "none.jpg",
"soundfile": "",
"user_id": "1"
}
},
{
"Word": {
"id": "2",
"word": "computer",
"word_syllables": "com-pute-r",
"picture": "computer.jpg",
"soundfile": "",
"user_id": "0"
}
}
]
}
我认为上面的内容可以通过一个名为" Words"其中包含一个arraylist / Word对象列表。
Words类;
package com.example.testgson.business;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
public class Words {
public List<Word> words=new ArrayList<Word>();
public Words(){
}
public int size(){
return words.size();
}
public void addWord(Word w){
this.words.add(w);
}
public List<Word> getWords() {
return words;
}
public void setWords(List<Word> words) {
this.words = words;
}
public void printAll(){
for(int i=0; i<words.size();i++){
Word w=(Word) words.get(i);
if(w!=null){
Log.d("word",w.getWord());
}
}
}
public List <Word> getWordList(){
return this.words;
}
}
Word Class;
public class Word {
int id;
String word;
String word_syllables;
String picture;
String soundfile;
int user_id;
public Word(){
}
public Word(int id, String word, String word_syllables, String picture,
String soundfile, int user_id) {
super();
this.id = id;
this.word = word;
this.word_syllables = word_syllables;
this.picture = picture;
this.soundfile = soundfile;
this.user_id = user_id;
}
@Override
public String toString() {
return "Word [id=" + id + ", word=" + word + ", word_syllables="
+ word_syllables + ", picture=" + picture + ", soundfile="
+ soundfile + ", user_id=" + user_id + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord() {
return this.word;
}
public void setWord(String word) {
this.word = word;
}
public String getWord_syllables() {
return word_syllables;
}
public void setWord_syllables(String word_syllables) {
this.word_syllables = word_syllables;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getSoundfile() {
return soundfile;
}
public void setSoundfile(String soundfile) {
this.soundfile = soundfile;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
}
GSON转换代码;
Gson gson = new Gson();
Words obj = gson.fromJson(sjson, Words.class);
答案 0 :(得分:2)
您需要跳过一个级别,因为您的数组不是List<Word>
而是List<Holder>
,其中Holder
类具有Word
实例。您可以创建此类,也可以编写自定义反序列化器来跳过它:
class CustomDeserializer implements JsonDeserializer<Word> {
@Override
public Word deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
JsonElement content = je.getAsJsonObject().get("Word");
return new Gson().fromJson(content, type);
}
}
然后:
Gson gson = new GsonBuilder().registerTypeAdapter(Word.class, new CustomDeserializer()).create();
产生:
Word [id=1, word=submarine, word_syllables=sub-mar-ine, picture=none.jpg, soundfile=, user_id=1]
Word [id=2, word=computer, word_syllables=com-pute-r, picture=computer.jpg, soundfile=, user_id=0]