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

标签 java json parsing gson

如何让 GSON 库将以下 JSON 字符串正确转换为对象。我已经尝试了很长时间,但它似乎只挑选出 2 个“Word”对象,并将成员字段留空或为 null。

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”的类来创建上面的内容,该类包含 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;
    }   
}

词类;

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类有一个 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]

关于java - 我怎样才能让GSON解析JSON字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29073398/

相关文章:

javascript - 在javascript中访问动态请求变量

java - java facebook-swift、thrift 用法示例

java - 如何获取 JTable 中列标题的字体和图形

javascript - 全日历在当前月份的月 View 中的错误日期显示事件

java - hibernate JTA : Read DB connection parameters per environment

javascript - js : please explain this variable value with 5 double quotations

json - 服务器过滤器 kendoui 调度程序和刷新

parsing - 如何解析未知长度的二进制 PDF 流?

java - 从 html 文件中获取信息

java - 如何以 scala 方式解析基于行的文本文件(.mht)?