简体   繁体   English

我如何让GSON解析JSON字符串

[英]How can i get GSON to parse JSON String

How can I get the GSON library to correctly convert the below JSON string to objects. 如何获取GSON库以将以下JSON字符串正确转换为对象。 I've tried for ages but it only seems to pick out the 2 "Word" objects and leave the member fields blank or null. 我已经尝试了很长时间,但它似乎只能挑选出2个“ Word”对象,并将成员字段保留为空白或null。

JSON: 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"
            }
        }
    ]
}

I thought that the above could be created simply by having a class called "Words" which contains an arraylist/list of Word objects. 我以为可以通过简单地创建一个名为“ Words”的类来创建以上内容,该类包含一个Word对象的arraylist / list。

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

The 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 convert code; GSON转换代码;

Gson gson = new Gson();         
Words obj = gson.fromJson(sjson, Words.class);  

You need to skip one level, because you array is not a List<Word> but a List<Holder> where the Holder class has a Word instance. 您需要跳过一个级别,因为数组不是List<Word>而是List<Holder> ,其中Holder类具有Word实例。 You can either create this class, or write a custom deserializer to skip it: 您可以创建此类,也可以编写自定义解串器以跳过该类:

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

and then: 接着:

Gson gson = new GsonBuilder().registerTypeAdapter(Word.class, new CustomDeserializer()).create();

which yields: 产生:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM