我怎样才能让 GSON 解析 JSON 字符串

How can i get GSON to parse JSON String

如何让 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" 的 class,其中包含 arraylist/list 个 Word 对象。

单词class;

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;
    }   
}

这个词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);  

您需要跳过一级,因为您的数组不是 List<Word>,而是 List<Holder>,其中 Holder class 有一个 Word 实例.您可以创建此 class,或编写自定义反序列化程序以跳过它:

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]