How do I perform basic Json operations: loop properties/array, get list of property names, build json object from scratch and ToString everything :| thanks!

I am having much difficulty using the json especially because I am new to C++. Sure, did some in highschool and am a programmer but you must admit the documentation is a bit lacking in basic examples. I do find bits and pieces here and there. Found how to parse from string to Json but that’s about it.

I found this library here and it looks nice and clean to me, and the examples cover all operations:
https://code.google.com/p/jsonplus/wiki/Example

But I would like something similar in the native Json classes, if possible. Thank you!

You can declare a new JSON object in your header file as follows:

// Our json object containing the data
TSharedPtr<FJsonObject> SaveData;

and create it in your cpp file as follows:

SaveData = MakeShareable(new FJsonObject());

Adding a string value:

SaveData->SetStringField(name, value);

Saving to file:

void UPWNSerialiser::SaveToFile(FString fileName)
{
	// Convert save game json object to string
	FString SaveGameStringData;

	// Writer
	TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&SaveGameStringData);
	FJsonSerializer::Serialize(SaveData.ToSharedRef(), JsonWriter);
	FFileHelper::SaveStringToFile(*SaveGameStringData, *fileName);
}

Loading a JSON Object from file:

void UPWNSerialiser::LoadFromFile(FString fileName)
{
	FString LoadGameStringData;

	FFileHelper::LoadFileToString(LoadGameStringData, *fileName);
	TSharedRef< TJsonReader<> > JsonReader = TJsonReaderFactory<>::Create(LoadGameStringData);	
	bool readSuccess = FJsonSerializer::Deserialize(JsonReader, SaveData);	

	if (readSuccess)
	{
		UPWNGameGlobals::PrintOnscreenString("Successfully loaded save file.");
	}
	else
	{
		UPWNGameGlobals::PrintOnscreenString("Error loading save file.");
	}
}

Reading a value from the loaded JSON object:

value = SaveData->GetStringField(name);

you touched absolutely nothing from my question.

Your question is as vague as your comment is rude. Go figure it out yourself then.


1 Like

loop properties/array, get list of property names, build json object from scratch and ToString everything. Tell me, how is this vague?

He told you all basic operations. I would have done the same after reading your question. So it seems to be vague if this isn’t what you want to know.
The only thing he didn’t cover was the array/loop question.
Maybe just ask nicely to get this explained too, instead of posting that kind of answer to someone who wants to help you.

If you had bothered to read the answer you would see that 2 out of your 4 questions were answered, even with some extra details.

As for the short novel you rambled in the description: if you want that library, implement it yourself. I’m not getting paid for this, I’m out.

You’ve definitely saved me a couple of hours with what you’ve posted above, so I for one appreciate your contribution here greatly.

Logged in just to post this: Thank you, static voidlol. This helped me very much.

@staticvoidlol Your answer covers the basic :), can you help me for the loops / array objects in the Json? If you know some website or link that already has that, that would be great. Thanks in advance.

PS: I don’t know I am unable to like your answer.

Hi silentkratos,

I’ll have a look at this next weekend. Some ideas that might be worth exploring:

FJsonObject has a member called “Values” which is of type TMap which is a container for key/value pairs (I think). These objects can be iterated over using C++ 11 syntax:

for (auto currObject = myJsonObject->Values.CreateConstIterator(); currObject; ++currObject)
{

        // Some magic code that uses currObject
}

Might just be what you’re looking for.

Had a quick look in the UE source (unfortunately a very old version, 4.1), and it seems this is very possibly what you’re looking for (I haven’t tested it though) - check out:
Source/Runtime/Core/Private/Internationalization/InternationalizationMetadataJsonSerializer.cpp (function JSonValueToLocMetaDataValue, in the 4th case statement). I’ve extracted it below:

// Create test Json Object
TSharedPtr<FJsonObject> jsonData = MakeShareable(new FJsonObject());

// Add some string values:
jsonData->AddStringValue(TEXT("testkey1"),TEXT("testval1"));
jsonData->AddStringValue(TEXT("testkey2"),TEXT("testval2"));

// Iterate over Json Values
for (auto currJsonValue = jsonData->Values.CreateConstIterator(); currJsonValue; ++currJsonValue)
{
	// Get the key name
	const FString Name = (*currJsonValue).Key;
	
	// Get the value as a FJsonValue object
	TSharedPtr< FJsonValue > Value = (*currJsonValue).Value;
	
	// Do your stuff with crazy casting and other questionable rituals
}

Hi silentkratos,

I don’t know if you saw this (as I replied to my own comment) but I did manage to find out how to iterate over Json objects. See the other comment.

Cheers.

Very nice solutions staticvoidlol. I used this stuff to help my understanding of the json serializing/deserializing and now I figured I’d share what I learned in this already big thread of useful, json-specific information. The following answer is to how to deserialize a list of json objects from a json string. I hope this helps people like this thread helped me.

Hi staticvoidlol,

I see you have some experience with Json, do you know if it is possible to add an array of FJsonObjects to a main FJsonObject? I posted a question with more details about this. Thanks!

Definitely helped me with my problem. Thanks man!

Lifesaver answer, even tho this is an old thread, I wanna thank you a lot!