Using Json in Unreal Engine 4 – Part 2

In the last post, we had a look at how to serialize Json in Unreal Engine 4 – that is, converting a C++ Json object into an actual Json string that we can do something with, such as send to a web API.

This time we’re going to look at the opposite, which is deserializing Json – taking a Json string and putting it into a C++ Json object that we can then read properties from.

Deserializing is significantly quicker and easier than serializing.

Let’s use the weapon example from the first post that we want to deserialize.

Similar to the serialization process, we first need an FJsonObject to work with

Where we used a TJsonWriter and TJsonWriterFactory for serialization, we now want to use a TJsonReader and TJsonReaderFactory for the deserialization process as such:

JsonString is simply an FString containing the Json that we are deserializing, which would be the Json at the start of this article.

The next step is to then deserialize the Json. Rather than using the Serialize function in the FJsonSerializer class, we simply use the Deserialize function. The Deserialize function returns whether the Json was successfully deserialized or not.

Note the two parameters that we pass to the deserialize function Рthe Reader,  and the JsonObject we want to deserialize into.

If the deserialization is successful, then we can simply read out the values from the JsonObject!

JsonObject->GetStringField( TEXT("Name"));¬†will return “Super Sword”.

That’s it for simple Json deserialization! In the next post I’ll look at using some of the more advanced utilities available for Json in UE4.

 

Leave a Reply