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.
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.
@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.
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
}
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
}
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.
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!