【问题标题】:How to write complex string into Gson classes如何将复杂的字符串写入 Gson 类
【发布时间】:2018-07-08 16:39:30
【问题描述】:

我一直在绞尽脑汁试图让 pojos 使用以下 json 字符串。我一直在做的一件事是在 pojo 中制作多个 pojo,类似于在 spring boot 中所必需的。不确定 GSON 是否也需要这样做。经过多次尝试,我仍然没有得到它。感谢您提前提供任何建议。

Json 字符串:

"results": [
        {
            "id": "superficial",
            "language": "en",
            "lexicalEntries": [
                {
                    "derivatives": [
                        {
                            "id": "superficialness",
                            "text": "superficialness"
                        }
                    ],
                    "entries": [
                        {
                            "etymologies": [
                                "late Middle English: from late Latin superficialis, from Latin superficies (see superficies)"
                            ],
                            "grammaticalFeatures": [
                                {
                                    "text": "Positive",
                                    "type": "Degree"
                                }
                            ],
                            "homographNumber": "000",
                            "senses": [
                                {
                                    "definitions": [
                                        "existing or occurring at or on the surface"
                                    ],
                                    "examples": [
                                        {
                                            "text": "the building suffered only superficial damage"
                                        }
                                    ],
                                    "id": "m_en_gbus1014360.017",
                                    "short_definitions": [
                                        "existing or occurring at or on surface"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "situated or occurring on the skin or immediately beneath it"
                                            ],
                                            "domains": [
                                                "Anatomy",
                                                "Medicine"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "the superficial muscle groups"
                                                }
                                            ],
                                            "id": "m_en_gbus1014360.019",
                                            "short_definitions": [
                                                "situated or occurring on skin or immediately beneath it"
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "appearing to be true or real only until examined more closely"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "the resemblance between the breeds is superficial"
                                                }
                                            ],
                                            "id": "m_en_gbus1014360.020",
                                            "short_definitions": [
                                                "appearing to be true or real only until examined more closely"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "superficial",
                                                    "sense_id": "t_en_gb0014419.004"
                                                }
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "not thorough, deep, or complete; cursory"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "he had only the most superficial knowledge of foreign countries"
                                                }
                                            ],
                                            "id": "m_en_gbus1014360.021",
                                            "short_definitions": [
                                                "not thorough or complete"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "superficial",
                                                    "sense_id": "t_en_gb0014419.003"
                                                }
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "not having or showing any depth of character or understanding"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "perhaps I was a superficial person"
                                                }
                                            ],
                                            "id": "m_en_gbus1014360.022",
                                            "short_definitions": [
                                                "not having or showing any depth of character or understanding"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "superficial",
                                                    "sense_id": "t_en_gb0014419.006"
                                                },
                                                {
                                                    "entry_id": "superficial",
                                                    "sense_id": "t_en_gb0014419.002"
                                                }
                                            ]
                                        }
                                    ],
                                    "thesaurusLinks": [
                                        {
                                            "entry_id": "superficial",
                                            "sense_id": "t_en_gb0014419.001"
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Adjective",
                    "pronunciations": [
                        {
                            "dialects": [
                                "American English"
                            ],
                            "phoneticNotation": "respell",
                            "phoneticSpelling": "ˌso͞opərˈfiSHəl"
                        },
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/superficial_us_1.mp3",
                            "dialects": [
                                "American English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "ˌsupərˈfɪʃəl"
                        }
                    ],
                    "text": "superficial"
                }
            ],
            "type": "headword",
            "word": "superficial"
        }

Pojos:

class Results {

    private String id;
    private String language;
    private String lexicalEntries;
    private String type;
    private String word;

//
}

class Derivatives {

    private String id;
    private String text;
//
    }


class Entries {

    private String etymologies;
    private String grammaticalFeatures;
    private int homographNumber;
    private List<String> senses;
    //
}

public class LexicalEntries {

    private Derivatives derivatives;
    private List<Entries> entries;
    private String language;
    private String lexicalCategory;
    private List<Pronunciations> pronunciations;
    private String text;
//
}

public class Pronunciations {

    private String dialects;
    private String phoneticNotation;
    private String phoneticSpelling;
//
}

class Senses {

    private String definitions;
    private String examples;
    private String id;
    private ShortDefinitions short_definitions;
    private List<Subsenses> subsenses;
    private List<ThesaurusLinks> thesaurusLinks;
//

}

class ShortDefinitions {

    private String short_definitions;
//
}

class Subsenses {

    private String definitions;
    private String domains;
    private String examples;
    private String id;
    private String shortDefinitions;
    private String thesaurusLinks;
//
}

class ThesaurusLinks {
    private String entry_id;
    private String sense_id;
//
}

【问题讨论】:

  • 构建对应的类有什么问题?
  • 您好,我已经添加了我的类,但我得到了一个空指针。我假设我的课程是错误的。注意我有所有的 setter 和 getter。

标签: java json gson


【解决方案1】:

我已根据您的 JSON 更正了您的 POJO 类定义。

我建议您阅读 JSON 表示法,了解不同的数据类型如何在其中表示,例如 Array/List、Object、String、int、boolean。它将阐明您的理解,您将知道为什么您的类没有正确映射到 JSON 数据模式。

class Results {

    private String id;
    private String language;
    private List<LexicalEntry> lexicalEntries;
    private String type;
    private String word;

//
}

public class LexicalEntry {

    private List<Derivative> derivatives;
    private List<Entry> entries;
    private String language;
    private String lexicalCategory;
    private List<Pronunciation> pronunciations;
    private String text;
//
}

class Derivative {

    private String id;
    private String text;
//
}


class Entry {

    private List<String> etymologies;
    private List<GrammaticalFeature> grammaticalFeatures;
    private String homographNumber; //if it has quotes in JSON, it will be a string not int..JSON is a strongly typed object notation
    private List<Sense> senses;
    //
}

class GrammaticalFeature{
    String text;
    String type;
}



public class Pronunciation {
    private String audioFile;
    private List<String> dialects;
    private String phoneticNotation;
    private String phoneticSpelling;
//
}

class Sense {

    private List<String> definitions;
    private List<Example> examples;
    private String id;
    private List<String> short_definitions;
    private List<Subsense> subsenses;
    private List<ThesaurusLinks> thesaurusLinks;
//

}

class Example{
    String text;
}

class Subsense {

    private List<String> definitions;
    private List<String> domains;
    private List<Example> examples;
    private String id;
    private List<String> shortDefinitions;
    private List<ThesaurusLink> thesaurusLinks;
//
}

class ThesaurusLink {
    private String entry_id;
    private String sense_id;
//
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多