反序列化多维JSON字符串

4

我是一个新手,所以请原谅任何不规范的问题程序!

基本上,我正在尝试从Pearson Dictionary Web API反序列化一个json数组。以下是JSON(我删除了一些results索引以节省空间):

{
  "status": 200,
  "offset": 0,
  "limit": 10,
  "count": 10,
  "total": 47,
  "url": "/v2/dictionaries/ldoce5/entries?headword=test",
  "results": [
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "test",
      "homnum": 1,
      "id": "cqAFzDfHTM",
      "part_of_speech": "noun",
      "pronunciations": [
        {
          "audio": [
            {
              "lang": "British English",
              "type": "pronunciation",
              "url": "/v2/dictionaries/assets/ldoce/gb_pron/brelasdetest.mp3"
            },
            {
              "lang": "American English",
              "type": "pronunciation",
              "url": "/v2/dictionaries/assets/ldoce/us_pron/test1.mp3"
            }
          ],
          "ipa": "test"
        }
      ],
      "senses": [
        {
          "definition": [
            "a set of questions, exercises, or practical activities to measure someone's skill, ability, or knowledge"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-001626298.mp3"
                }
              ],
              "text": "Did you get a good mark in the test ?"
            }
          ],
          "gramatical_examples": [
            {
              "examples": [
                {
                  "audio": [
                    {
                      "type": "example",
                      "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-000592041.mp3"
                    }
                  ],
                  "text": "We have a test on irregular verbs tomorrow."
                }
              ],
              "pattern": "test on"
            }
          ],
          "signpost": "exam"
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFzDfHTM"
    }
  ]
}

以下是我使用的 C# 代码来反序列化上述内容:

class Program
    {
        static void Main(string[] args)
        {
            string word = "test";

            string sURL = "https://api.pearson.com:443/v2/dictionaries/ldoce5/entries?headword=" + word;

            WebClient client = new WebClient();
            string full = client.DownloadString(sURL);

            var final = JsonConvert.DeserializeObject<Dictionary>(full);

            Console.WriteLine(final.results[0].senses.definition);
        }
    }

    public class Dictionary
    {
        public Result[] results { get; set; }
    }

    public class Result
    {
        public string part_of_speech { get; set; }
        public Senses senses { get; set; }
    }

    public class Senses
    {
        public string definition { get; set; }
    }

出现了奇怪的错误,如下所示:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'TestingJson.Senses',因为该类型需要一个 JSON 对象(例如 {"name":"value"})才能正确反序列化。要解决此错误,请将 JSON 更改为 JSON 对象(例如{"name":"value"}),或将反序列化类型更改为数组或实现集合接口(例如 ICollection、IList)的类型,例如可以从 JSON 数组反序列化的 List。也可以向类型添加 JsonArrayAttribute,以强制从 JSON 数组反序列化。路径'results[0].senses',行 1,位置 512。
希望能得到帮助!

不要强制使用字典,让它自己创建对象。 - Blindy
“Dictionary” 是我自己写的类,不是 .NET 的 Dictionary。 - Destroyer97
“Senses”是单数类型。 “senses” JSON节点是复数(数组)...您不能将一个复数形状的对象塞进单数形状的孔中。 - Sam Axe
1个回答

6
如果你正在与某个明确定义的东西交互(例如,大多数API),那么创建一个强类型对象比使用动态或字典要好得多。
在Visual Studio中,如果你转到“编辑 > 特殊粘贴 > 将JSON粘贴为类”,它将生成你需要的所有对象。
public class Rootobject
{
    public int status { get; set; }
    public int offset { get; set; }
    public int limit { get; set; }
    public int count { get; set; }
    public int total { get; set; }
    public string url { get; set; }
    public Result[] results { get; set; }
}

public class Result
{
    public string[] datasets { get; set; }
    public string headword { get; set; }
    public int homnum { get; set; }
    public string id { get; set; }
    public string part_of_speech { get; set; }
    public Pronunciation[] pronunciations { get; set; }
    public Sens[] senses { get; set; }
    public string url { get; set; }
}

public class Pronunciation
{
    public Audio[] audio { get; set; }
    public string ipa { get; set; }
}

public class Audio
{
    public string lang { get; set; }
    public string type { get; set; }
    public string url { get; set; }
}

public class Sens
{
    public string[] definition { get; set; }
    public Example[] examples { get; set; }
    public Gramatical_Examples[] gramatical_examples { get; set; }
    public string signpost { get; set; }
}

public class Example
{
    public Audio1[] audio { get; set; }
    public string text { get; set; }
}

public class Audio1
{
    public string type { get; set; }
    public string url { get; set; }
}

public class Gramatical_Examples
{
    public Example1[] examples { get; set; }
    public string pattern { get; set; }
}

public class Example1
{
    public Audio2[] audio { get; set; }
    public string text { get; set; }
}

public class Audio2
{
    public string type { get; set; }
    public string url { get; set; }
}

好的提示关于粘贴。您能否更新以指示支持的版本,以便在几年后仍然具有良好答案的上下文价值! - Mark Schultheiss
非常感谢viggity!是的,我使用的是Visual Studio 2010,并且没有“编辑>特殊粘贴”,所以感谢您在上面粘贴。哈哈 :P - Destroyer97
不确定是从什么时候开始引入的,我使用的是VS2013,但是我检查了一下VS2012,那里也有这个选项。 - viggity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接