简体   繁体   中英

Deserializing a multidimensional JSON string

I'm new here, so please excuse any lapses in proper question procedure!

Basically, I'm trying to deserialize a json array from the Pearson Dictionary Web API. Here's the JSON (I removed some excess results indices to conserve space):

{
  "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"
    }
  ]
}

And here is the C# code I'm using to deserialize the above:

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

For some reason, I'm getting this strange error when I try to run it:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'TestingJson.Senses' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'results[0].senses', line 1, position 512.

Help would be greatly appreciated!

If you're interacting with something well defined (ie the vast majority of APIs out there), then you're much better off creating a strongly typed object(s) instead of dynamic or dictionary.

In Visual Studio if you go Edit>Paste Special>Paste JSON as Classes then it will generate all the objects you need.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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