broly
(broly)
August 2, 2015, 6:04am
1
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);
}
ufna
(ufna)
August 3, 2015, 7:33am
2
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
broly
(broly)
August 4, 2015, 5:44pm
3
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
Hello,
I had quite same question, and I solved it by myself, if you want to check:
Hello, I have a json structured with several arrays imbricated. I ve started such code, but I don’t know how to manage the second level of arrays Can you help, plz to fill my code? if (FJsonSerializer::Deserialize(JsonReader, JsonParsed)) {...
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.
LucasNova
(LucasNova)
October 28, 2019, 7:41am
7
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 .