如何调试对象到类的赋值?

时间:2016-05-27 10:22:52

标签: c#

我正在尝试按如下方式分配此对象:

RootObject aaa = new RootObject() {
  "word": "example",
  "results": [
    {
      "definition": "a representative form or pattern",
      "partOfSpeech": "noun",
      "synonyms": [
        "model"
      ],
      "typeOf": [
        "representation",
        "internal representation",
        "mental representation"
      ],
      "hasTypes": [
        "prefiguration",
        "archetype",
        "epitome",
        "guide",
        "holotype",
        "image",
        "loadstar",
        "lodestar",
        "microcosm",
        "original",
        "paradigm",
        "pilot",
        "prototype",
        "template",
        "templet",
        "type specimen"
      ],
      "derivation": [
        "exemplify"
      ],
      "examples": [
        "I profited from his example"
      ]
    },
    {
      "definition": "something to be imitated",
      "partOfSpeech": "noun",
      "synonyms": [
        "exemplar",
        "good example",
        "model"
      ],
      "typeOf": [
        "ideal"
      ],
      "hasTypes": [
        "pacemaker",
        "pattern",
        "beauty",
        "prodigy",
        "beaut",
        "pacesetter"
      ],
      "derivation": [
        "exemplify",
        "exemplary"
      ]
},
    {
      "definition": "an occurrence of something",
      "partOfSpeech": "noun",
      "synonyms": [
        "case",
        "instance"
      ],
      "typeOf": [
        "happening",
        "natural event",
        "occurrence",
        "occurrent"
      ],
      "hasTypes": [
        "clip",
        "mortification",
        "piece",
        "time",
        "humiliation",
        "bit"
      ],
      "derivation": [
        "exemplify"
      ],
      "examples": [
        "but there is always the famous example of the Smiths"
      ]
    },
    {
      "definition": "an item of information that is typical of a class or group",
      "partOfSpeech": "noun",
      "synonyms": [
        "illustration",
        "instance",
        "representative"
      ],
      "typeOf": [
        "information"
      ],
      "hasTypes": [
        "excuse",
        "apology",
        "specimen",
        "case in point",
        "sample",
        "exception",
        "quintessence",
        "precedent"
      ],
      "derivation": [
        "exemplify",
        "exemplary"
      ],
      "examples": [
        "this patient provides a typical example of the syndrome",
        "there is an example on page 10"
      ]
    },
    {
      "definition": "punishment intended as a warning to others",
      "partOfSpeech": "noun",
      "synonyms": [
        "deterrent example",
        "lesson",
        "object lesson"
      ],
      "typeOf": [
        "monition",
        "admonition",
        "word of advice",
        "warning"
      ],
      "derivation": [
        "exemplary"
      ],
      "examples": [
        "they decided to make an example of him"
      ]
    },
    {
      "definition": "a task performed or problem solved in order to develop skill or understanding",
      "partOfSpeech": "noun",
      "synonyms": [
        "exercise"
      ],
      "typeOf": [
        "lesson"
      ],
      "examples": [
        "you must work the examples at the end of each chapter in the textbook"
      ]
    }
  ],
  "syllables": {
    "count": 3,
    "list": [
      "ex",
      "am",
      "ple"
    ]
  },
  "pronunciation": {
    "all": "ɪɡ'zæmpəl"
  }
}

以下是我定义的类:

        public class Result
{
    public string definition { get; set; }
    public string partOfSpeech { get; set; }
    public List<string> synonyms { get; set; }
    public List<string> typeOf { get; set; }
    public List<string> hasTypes { get; set; }
    public List<string> derivation { get; set; }
    public List<string> examples { get; set; }
}

public class Syllables
{
    public int count { get; set; }
    public List<string> list { get; set; }
}

public class Pronunciation
{
    public string all { get; set; }
}

public class RootObject
{
    public string word { get; set; }
    public List<Result> results { get; set; }
    public Syllables syllables { get; set; }
    public Pronunciation pronunciation { get; set; }
}

我从#34; word&#34;开始在第一行开始出现语法错误。

有没有办法可以调试这个或至少找出造成问题的原因?

2 个答案:

答案 0 :(得分:2)

尝试

RootObject aaa = JsonConvert.DeserializeObject<RootObject>(
    put_your_string_from_the_question);

从nuget.org下载Json.Net

答案 1 :(得分:1)

您根本无法将JSON对象分配给C#Object。您需要指定类的属性并将其DeSerialize为JSON格式。

在你的情况下,你应该这样做:

var roorObj= new RootObject()
{
 word ="your word",
 //
 //
}

然后要获取JOSN字符串,请使用:

 RootObject aaa = JsonConvert.DeserializeObject<RootObject>(roorObj);

希望你能想到使用C#对象和JSON。