How to parse json array?

I need to parse json array like [“qwe”, 12, “3345”, 0, 0.4]
What can I do with it?

Standard JsonReader not helps or I don’t fully understand how to use them.

	TSharedPtr<FJsonValueArray> JsonParsed;
	TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonRaw);
	//FJsonObjectConverter::
	if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
	{
		FString ExampleString = JsonParsed->GetStringField("");
		PRINT("%s", *ExampleString);
	}

FString JsonString = TEXT(“YOUR_JSON_STRING”);
TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(*JsonString);
if (FJsonSerializer::Deserialize(Reader, JsonObj) && JsonObj.IsValid())
{
// All is okay, json is valid
JsonObj->GetArrayField(…); // get the array field
// @todo process it now as u want
}

Hope this will help :wink:

What is “…” param of GetArrayField?
GetArrayField returns array by key of JsonObject. But this is array, has no any keys. Only indices.

I do next:

FString JsonRaw = FString::Printf(TEXT("{\"data\": %s}"), *JsonString);
 TSharedPtr<FJsonObject> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonRaw);
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
{
            auto arr = JsonParsed->GetArrayField("data");
}

“arr” is array converted from JsonString.
But it is awful code. If no any ways to do it more pretty… I use my ■■■■-code (~;~)

But thank you :slight_smile:

Hello,

I had quite same question, and I solved it by myself, if you want to check:

Hi,

This question is still unanswered, thyshimrod’s solution assumes nested array and the OP asks about the root array.

Here’s what you should do, I believe:

FString rawJson = "[\"qwe\", 12, \"3345\", 0, 0.4]";
TSharedPtr<FJsonValue> jsonParsed;

TSharedRef<TJsonReader<TCHAR>> jsonReader = TJsonReaderFactory<TCHAR>::Create(rawJson);

if (FJsonSerializer::Deserialize(jsonReader, jsonParsed))
{
    TSharedPtr<FJsonObject> firstObject = jsonParsed->AsArray()[0]->AsObject();
    // ...
}

Please note that instead of FJsonObject a more generic class FJsonValue is used, which may be converted to an array or other objects.

1 Like

There may be erros when use root as array? I compiled right while warning “Json Value of type ‘Object’ used as a ‘Array’.” when use.

You may know, but this is bad json you must know about every indexed item context, this is bad practice of json data structure. If you json have more conventional structure you can use a little online tool which can generate all of you want for work with json in unreal engine 4, tool supports arrays, nested json and can correctly recognize types - json2ue.com.