How to get specific data from a JSON file?

I’m working on a word game that after each match, sends out a request to a link on this website.. It then returns a string in JSON format. This works!

However I am now left with a JSON file (that’s a string) that looks like this (example):

[
    {
      "word": "hello",
      "phonetic": "həˈləʊ",
      "phonetics": [
        {
          "text": "həˈləʊ",
          "audio": "//ssl.gstatic.com/dictionary/static/sounds/20200429/hello--_gb_1.mp3"
        },
        {
          "text": "hɛˈləʊ"
        }
      ],
      "origin": "early 19th century: variant of earlier hollo ; related to holla.",
      "meanings": [
        {
          "partOfSpeech": "exclamation",
          "definitions": [
            {
              "definition": "used as a greeting or to begin a phone conversation.",
              "example": "hello there, Katie!",
              "synonyms": [],
              "antonyms": []
            }
          ]
        },
        {
          "partOfSpeech": "noun",
          "definitions": [
            {
              "definition": "an utterance of ‘hello’; a greeting.",
              "example": "she was getting polite nods and hellos from people",
              "synonyms": [],
              "antonyms": []
            }
          ]
        },
        {
          "partOfSpeech": "verb",
          "definitions": [
            {
              "definition": "say or shout ‘hello’.",
              "example": "I pressed the phone button and helloed",
              "synonyms": [],
              "antonyms": []
            }
          ]
        }
      ]
    }
  ]

I need to know how to get specific pieces of data from this.

Here’s the code I have:

    IEnumerator GetInfo(string word)
    {
        var request = UnityWebRequest.Get("https://api.dictionaryapi.dev/api/v2/entries/en/" + word.ToLower());
        yield return request.SendWebRequest();


        if(request.result != UnityWebRequest.Result.Success)
        {
            print("Error Fetching results");
        }
        else
        {
            string definition = request.downloadHandler.text; //Works
            print(definition);

            //GET DATA FROM JSON
        }
    }

I looked on the Unity Documentation, but really couldn’t find anything useful.

Any help would be appreciated!

You need to deserialise the JSON.

Use a site like this to write a C# compatible data structure: https://json2csharp.com/

Then just deserialise the string and pull out the data you need.

1 Like

Is there a way I could do this without using a website? I feel like that’s a lot fo data to keep sending out.

Also, just for learning purposes it wold be nice to know :slight_smile:

Um… the website is just to show you what C# structure you need to write to deserialise the data into. You just need to paste your JSON, and use the C# code it gives you (and modify it as necessary).

It’s a one and done kind of thing.

2 Likes

Ah, sorry. Misunderstanding on my part :slight_smile:

Okay, I plugged it in, but I’m still having a bit of trouble. Getting the error: ArgumentException: JSON must represent an object type.

This is what I got so far with my code:

using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class WordDefinition : MonoBehaviour
{
    public string word;


    void Start()
    {
        StartCoroutine(GetInfo(word));
    }

    void Update()
    {
        
    }

    IEnumerator GetInfo(string word)
    {
        var request = UnityWebRequest.Get("https://api.dictionaryapi.dev/api/v2/entries/en/" + word.ToLower());
        yield return request.SendWebRequest();


        if(request.result != UnityWebRequest.Result.Success)
        {
            print("Error Fetching results");
        }
        else
        {
            string definition = request.downloadHandler.text; //Works

            //GET DATA FROM JSON
            Root root = new Root();
            root = JsonUtility.FromJson<Root>(definition);

            print(root);

        }
    }
}


[System.Serializable]
public class Definition
{
    public string definition;
    public string example;
    public List<object> synonyms;
    public List<object> antonyms;
}

[System.Serializable]
public class Meaning
{
    public string partOfSpeech;
    public List<Definition> definitions;
}

[System.Serializable]
public class Phonetic
{
    public string text;
    public string audio;
}

[System.Serializable]
public class Root
{
    public string word;
    public string phonetic;
    public List<Phonetic> phonetics;
    public string origin;
    public List<Meaning> meanings;
}





Does it being uppercase matter? If the JSON has it “meanings” and I make a class “Meaning”, does that change anything?

Use Newtonsoft.JSON not JsonUtility. Unity’s JSON-lite implementation is not the same.

Okay, Ill look into implementing that!

I’m using Unity 6— is it available for that?

It’s a package.