Alan2
Alan2

Reputation: 24602

How can I debug the assignment of an object to a class?

I am trying to assign this object as follows:

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

Here are the classes I defined:

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

I'm getting syntax errors starting on the first line with "word".

Is there a way I can debug this or at least find out what is causing the problem?

Upvotes: 0

Views: 43

Answers (2)

error_handler
error_handler

Reputation: 1201

You simply cannot assign JSON object to C# Object. You need to assign properties of your class and DeSerialize that to JSON format.

in your case you should do like this way:

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

Then to get the JOSN string, use this:

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

Hope you will get some idea to play with C# object and JSON.

Upvotes: 1

rudolf_franek
rudolf_franek

Reputation: 1885

Try

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

Download Json.Net from nuget.org

Upvotes: 2

Related Questions