0

Given a block of JSON such as below,

{
  "word": "dog",
  "definitions": [
    {
      "definition": "a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll",
      "partOfSpeech": "noun"
    },
    {
      "definition": "go after with the intent to catch",
      "partOfSpeech": "verb"
    }
  ]
}

I know how use Unity's built in JsonUtility to create a class to parse a string...

WORDCLASS json = WORDCLASS.CreateFromJSON(someJSONstring);
  
            print(json.word);

Example class for JsonUtility

[System.Serializable]
public class WORDCLASS
{
    public string word;

    public static WordJSON CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<WordJSON>(jsonString);
    }
}

I understand that "definitions" is an array of Dictionaries (with keys "definition" and "partOfSpeech" ... I just don't understand the correct way to get to them... (i.e. I would like to loop through and print out all the definitions in the array...

Although I'm attempting to use the built in Unity JsonUtility, I am open to other (more efficient) approaches such as Newton Json...

Thanks for your time helping out.

timothykc
  • 2,235
  • 1
  • 16
  • 14
  • *I understand that "definitions" is an array of Dictionaries* -- it looks like an array of classes instead, with two properties `public string definition { get; set; }` and `public string partOfSpeech { get; set; }`. Dictionaries are used when the key names are variable but their values have some fixed schema. – dbc Jul 05 '21 at 22:35
  • 1
    Though I seem to recall that `JsonUtility` only serializes public fields and not properties? The tools from [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182) should help you generate classes to deserialize that JSON. E.g. go to https://json2csharp.com/, paste in your JSON, check **`[x] Use Fields`** and push **`Convert`** and you will get a plausible set of classes. – dbc Jul 05 '21 at 22:39
  • @dbc thanks for link to json2csharp.com... that made everything crystal clear!!! – timothykc Jul 05 '21 at 22:49
  • it is not a dictionary but a simple list of objects each having 2 string fields: definition and partOfSpeech. – kolodi Jul 06 '21 at 07:14

1 Answers1

0

Using the json2csharp.com link provided by @DBC quickly parsed the class definitions for the following JSON...

{
  "word": "dog",
  "definitions": [
    {
      "definition": "a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll",
      "partOfSpeech": "noun"
    },
    {
      "definition": "go after with the intent to catch",
      "partOfSpeech": "verb"
    }
  ]
}

To the class structures below...

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    //level 1
    public class Definition
    {
        public string definition { get; set; }
        public string partOfSpeech { get; set; }
    }
    //level 0
    public class Root
    {
        public string word { get; set; }
        public List<Definition> definitions { get; set; }
    }


In essence, each level of the Json you wish to traverse requires its own class...

 Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJson);

            foreach (var v in myDeserializedClass.definitions)
            {
                print("Here is a definition: " + v.definition);
            }
timothykc
  • 2,235
  • 1
  • 16
  • 14