Made With Unity | Newtonsoft’s Json.NET

Rehtse Studio
5 min readOct 6, 2022

Newtonsoft’s Json.NET is a popular high-performance JSON framework for .NET that can be used with Unity; similar to Unity’s Json Utility but with other features.

In this short article, I will share a basic approach using Newtonsoft’s Json.NET to fetch data from a web API. We are going to get a joke from “https://icanhazdadjoke.com/api” that comes out as a JSON and show it on the Game Scene.

GitHub Link

https://github.com/rehtse-studio/FunWithJson_NewtonsoftJsonNET

The Unity Project

For this project, we are using Unity 2021 LTS which has some Game Objects, and a UI Canvas.

Jokes Game Object

The Jokes GO holds two monobehaviour scripts: GetJokeWebRequest, and Joke.

UI Canvas

The UI GO holds the UIManager script and inside the UI GO, we have a TextMeshPro object called joke_tmp and a Button object labeled getJoke_button.

The UIManager script has a Joke TMP field where the joke_tmp is used as a reference.

The On Click() section on the getjoke_button references a public method on the GetJokeWebRequest script called JokeRequestCoroutine, just drag the Joke GO to the empty field on the On Click().

Until now we have 3 scrips that we are using but we are missing one, it is called JokesParsinJSON. It is a simple class that we are going to use to decentralize the JSON.

Scripts

GetJokeWebRequest and UIManager

I’m holding on to the other two scripts since there are two options to use Newtonsoft’s JSON, I’m going to share it with you, but first, let's get the Newtonsoft package.

Getting Newtonsoft’s Json.NET Package On Unity

On Unity go to Windows->Package Manager, once the Package Manager window opens, go to Add package from git URL, type com.unity.nuget.newtonsoft-json press Add and done.

Building The Class To Decentralize The JSON

We will need a C# class to map the JSON object and match the actual fields, how do we do that?

If we go to “https://icanhazdadjoke.com/api” there is an example when we fetch a random joke as a JSON

https://icanhazdadjoke.com/api

We take that JSON joke, minus the $ curl line, and place it in our JSON to C# tool converter to have the class made for us, it is a nice helper.

As a reminder, the class needs to match the field from the JSON to work properly.

https://json2csharp.com/

Option A

In Unity, I created a JokesParsinJSON class script following the guidance from the converter. My class name is JokesParsinJSON VS on the converter is name Root; since I only need the joke string this is the only field in my class.

We will need to add the [Serializable] attribute on top of the class to have access to it, make sure to have using System; namespace above to enable the [Serializable] attribute.

Now we create the Joke script

On top of the Joke class, we added the namespace using Newtonsoft.Json; we create a private variable _jokesParsinJSON that has reference to our JokesParsinJSON class.

TakeJokeJSON() is a public method that is going to receive the JSON from the GetJokeWebRequest script.

Notice the following line:

_jokesParsinJSON = JsonConvert.DeserializeObject<JokesParsinJSON>(jsonText.ToString());

Using Newtonsoft’s JSON.NET we are using JsonConvert.DeserializeObject to reconstruct a series of strings to instantiate the object to be usable; hence, we need to add <JokesParsingJSON> to be the class with the properties that represent and have the same JSON properties.

We store the data on _jokesParsinJSON and now we have access to _jokesParsinJSON.joke to get the joke string to display it on the UI Text (joke_tmp) in Unity.

We put everything together and we got a simple and “funny” game app to show some jokes 😅:

Option B

The other option we can use Newtonsoft’s JSON.Net is to altogether ignore the idea that the class needs to have the properties written the same way as the JSON properties.

Let’s say there is a standard way that your public variable needs to be capitalized, but the JSON properties show that they are in lowercase; how do we approach this hiccup?

Newtonsoft’s JSON.NET has an attribute called [JsonProperty()], this in a way will allow us to link a variable that is written differently to a JSON property/field.

On the Joke class script, we add using Newtonsoft.Json to have access to the [JsonProperty()] attribute and added the attribute on top of the public string Joke. Even when Joke is written with a capitalized letter when adding the property the system knows that Joke is joke from the JSON.

Got to your Joke script, you are going to have an error on this script and change:

_jokesParsinJSON.joke.ToString()

from the _uiManager to

_jokesParsinJSON.Joke.ToString()

There you have it, a short and basic approach using Newtonsoft’s Json.NET to decentralize an API’s JSON response in Unity.

HAVE A LOVELY DAY😎

--

--

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.