簡體   English   中英

反序列化時確定適當的結構 Class JSON c#

[英]Determining Appropriate Class Structures when Deserializing JSON c#

每個主題行,我有一個較大的(300,000 多個條目)JSON,我想反序列化...我熟悉 JSONtoSharp 和其他有助於生成類的資源...

鑒於此 JSON 片段(下面的第二個代碼塊),JSONtoSharp 建議我使用以下結構(參見第一個代碼塊)

我的問題是我不知道 word3 -> word300,000 是什么,我也不想為每個定義一個 class :)

所以想知道是否有一些我可以使用的通用結構、轉換模式?

感謝您的時間和幫助。

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

您需要一個不同的結構來包含定義:

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

然后你可以像這樣反序列化它(如果你使用的是 Json.NET)

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

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM