解析Merriam-Webster DictionaryAPI的Json格式

时间:2019-10-18 15:26:45

标签: c# json api

我刚开始编程,在解析Merriam-Webster的DictionaryAPI(以JSON格式提供)提供的信息时遇到了一些问题。我不认为我的问题是重复的,因为在该线程中没有解释将复杂的json文件类型解析为自定义C#对象。如果像引用的线程一样,我的json可以简单地解析为一个列表(一些,但不是几乎所有,可以在下面的代码中完成),我就不会问这个问题。我的json文件具有由数组等组成的对象的数组,而且我不知道如何正确解析这样的内容。当我使用Quicktype.io创建自定义对象以匹配json文件时,即使Quicktype.io也会返回数百行代码,我觉得这既不是解析json文件的最佳方法,也没有增加我的能力。自己解析它们。

我目前通过以下方式从这种格式获取信息:

    public struct MetaData
    { 
        public string id { get; set; }
        public bool offensive { get; set; }
    }

    public struct  RootObject
    {       
        public string Date { get; set; }
        public MetaData Meta { get; set; }
        public string Hom { get; set; }
        [JsonProperty("fl")]
        public string PartOfSpeech { get; set; }
        public string[] Shortdef { get; set; }   
    }

string allDataString 
= new WebClient().DownloadString(Config.bot.dictionaryURI);

RootObject[] rootObjects = new RootObject[JsonConvert.DeserializeObject<RootObject[]>(allDataString).Length];

rootObjects = JsonConvert.DeserializeObject<RootObject[]>(allDataString);

我想访问的不仅仅是RootObject和MetaData级别的条目;我想到达JSON文件的一系列数组和对象中的“ dt”之类的内容,但是我不确定如何格式化我的结构以使反序列化器正确放置我需要访问的信息。如果您有更好的方法来编写代码,仍然可以从所需的JSON文件中提取任何信息,我将很高兴听到它。

以下是数组中单个对象(多个)的示例:

[
    {
        "meta": 
        {
            "id": "test:1",
            "uuid": "0e5228e3-40df-4dce-92e0-36748eb1c24c",
            "sort": "200122700",
            "src": "collegiate",
            "section": "alpha",
            "stems": [
                "test",
                "tests"
            ],
            "offensive": false
        },
        "hom": 1,
        "hwi": 
        {
            "hw": "test",
            "prs": [
                {
                    "mw": "\u02c8test",
                    "sound": {
                        "audio": "test0001",
                        "ref": "c",
                        "stat": "1"
                    }
                }
            ]
        },
        "fl": "noun",
        "def": 
        [
            {
                "sseq": 
                [
                    [
                        [
                            "pseq",
                            [
                                [
                                    "bs",
                                    {
                                        "sense": {
                                            "sn": "1 a",
                                            "dt": [
                                                [
                                                    "text",
                                                    "{bc}a means of {a_link|testing}: such as"
                                                ]
                                            ]
                                        }
                                    }
                                ],
                                [
                                    "sense",
                                    {
                                        "sn": "(1)",
                                        "dt": [
                                            [
                                                "text",
                                                "{bc}something (such as a series of questions or exercises) for measuring the skill, knowledge, intelligence, capacities, or aptitudes of an individual or group"
                                            ]
                                        ]
                                    }
                                ],
                                [
                                    "sense",
                                    {
                                        "sn": "(2)",
                                        "dt": [
                                            [
                                                "text",
                                                "{bc}a procedure, reaction, or reagent used to identify or characterize a substance or constituent"
                                            ]
                                        ]
                                    }
                                ]
                            ]
                        ],
                        [
                            "sense",
                            {
                                "sn": "b",
                                "dt": [
                                    [
                                        "text",
                                        "{bc}a positive result in such a test"
                                    ]
                                ]
                            }
                        ]
                    ],
                    [
                        [
                            "pseq",
                            [
                                [
                                    "sense",
                                    {
                                        "sn": "2 a (1)",
                                        "dt": [
                                            [
                                                "text",
                                                "{bc}a critical examination, observation, or evaluation {bc}{sx|trial||}"
                                            ]
                                        ],
                                        "sdsense": {
                                            "sd": "specifically",
                                            "dt": [
                                                [
                                                    "text",
                                                    "{bc}the procedure of submitting a statement to such conditions or operations as will lead to its proof or disproof or to its acceptance or rejection "
                                                ],
                                                [
                                                    "vis",
                                                    [
                                                        {
                                                            "t": "a {wi}test{\/wi} of a statistical hypothesis"
                                                        }
                                                    ]
                                                ]
                                            ]
                                        }
                                    }
                                ],
                                [
                                    "sense",
                                    {
                                        "sn": "(2)",
                                        "dt": [
                                            [
                                                "text",
                                                "{bc}a basis for evaluation {bc}{sx|criterion||}"
                                            ]
                                        ]
                                    }
                                ]
                            ]
                        ],
                        [
                            "sense",
                            {
                                "sn": "b",
                                "dt": [
                                    [
                                        "text",
                                        "{bc}an ordeal or oath required as proof of conformity with a set of beliefs"
                                    ]
                                ]
                            }
                        ],
                        [
                            "sense",
                            {
                                "sn": "c",
                                "sls": [
                                    "chiefly British"
                                ],
                                "dt": [
                                    [
                                        "text",
                                        "{bc}{sx|cupel||}"
                                    ]
                                ]
                            }
                        ]
                    ],
                    [
                        [
                            "sense",
                            {
                                "sn": "3",
                                "dt": [
                                    [
                                        "text",
                                        "{bc}a result or value determined by testing"
                                    ]
                                ]
                            }
                        ]
                    ],
                    [
                        [
                            "sense",
                            {
                                "sn": "4",
                                "dt": [
                                    [
                                        "text",
                                        "{bc}{sx|test match||}"
                                    ]
                                ]
                            }
                        ]
                    ]
                ]
            }
        ],
        "et": [
            [
                "text",
                "Middle English, vessel in which metals were assayed, potsherd, from Anglo-French {it}test, tees{\/it} pot, Latin {it}testum{\/it} earthen vessel; akin to Latin {it}testa{\/it} earthen pot, shell"
            ]
        ],
        "date": "14th century{ds||2|c|}",
        "shortdef": [
            "a means of testing: such as",
            "something (such as a series of questions o7r exercises) for measuring the skill, knowledge, intelligence, capacities, or aptitudes of an individual or group",
            "a procedure, reaction, or reagent used to identify or characterize a substance or constituent"
        ]
    },
...

0 个答案:

没有答案