简体   繁体   中英

Determining Appropriate Class Structures when Deserializing JSON c#

Per subject line, I've got a largish (300,000+ entries) JSON that I would like to deserialize... I'm familiar with JSONtoSharp and other resources that help generate classes...

Given this JSON snippet (second block of code below), JSONtoSharp recommends I use the following structure (see first block of code)

My problem is that I don't know what word3 -> word300,000 are, nor would I want to define a class for each of them:)

So wondering if there is some generic structure, conversion pattern that I can use?

Thanks for your time and help.

 public class Definition
    {
        public string definition { get; set; }
        public string partOfSpeech { get; set; }
        public List<string> synonyms { get; set; }
        public List<string> similarTo { get; set; }
        public List<string> typeOf { get; set; }
    }

    public class Word1
    {
        public List<Definition> definitions { get; set; }
        public int letters { get; set; }
    }

    public class Word2
    {
        public List<Definition> definitions { get; set; }
        public int letters { get; set; }
    }

    public class Root
    {
        public Word1 word1 { get; set; }
        public Word2 word2 { get; set; }
    }
 {
    "word1": {
        "definitions": [
          {
            "definition": "being ten more than fifty",
        "partOfSpeech": "adjective",
        "synonyms": [
          "lx",
          "sixty",
          "threescore"
            ],
        "similarTo": [
          "cardinal"
            ]
      },
      {
            "definition": "the cardinal number that is the product of ten and six",
        "partOfSpeech": "noun",
        "synonyms": [
          "lx",
          "sixty"
        ],
        "typeOf": [
          "large integer"
        ]
      }
    ],
    "letters": 2
    },
  "word2": {
        "definitions": [
          {
            "definition": "being eight more than seventy",
        "partOfSpeech": "adjective",
        "synonyms": [
          "lxxviii",
          "seventy-eight"
            ],
        "similarTo": [
          "cardinal"
            ]
      },
      {
            "definition": "the cardinal number that is the sum of seventy and eight",
        "partOfSpeech": "noun",
        "synonyms": [
          "lxxviii",
          "seventy-eight"
        ],
        "typeOf": [
          "large integer"
        ]
      },
      {
            "definition": "a shellac based phonograph record that played at 78 revolutions per minute",
        "partOfSpeech": "noun",
        "synonyms": [
          "seventy-eight"
        ],
        "typeOf": [
          "disc",
          "disk",
          "platter",
          "record",
          "phonograph record",
          "phonograph recording"
        ]
      }
    ],
    "letters": 2
  }
}

You need a different structure to contain the definitions:

public class Definitions {
    public List<Definition> definitions { get; set;}
}

public class Definition {
    public string definition { get; set; }
    public string partOfSpeech { get; set; }
    public List<string> synonyms { get; set; }
    public List<string> similarTo { get; set; }
    public List<string> typeOf { get; set; }
}

Then you can deserialize it like this (if you're using Json.NET)

var definitions = JsonConvert.DeserializeObject<Dictionary<String, Definitions>>(json);

Result:

在此处输入图像描述

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