Hello, I have been trying to implement a client-server request-response module in my game.
I have been able to setup the request making and response fetching from the server as of now.
I get a proper response from the server, but the JSON output sent by the server is not being parsed by the following code I am using.
I referred to several questions posted in this forum already, but none helped this problem.
My code:
TSharedPtr<FJsonObject> JsonParsed = MakeShareable(new FJsonObject());
MessageBody.RemoveFromStart("[");
MessageBody.RemoveFromEnd("]");
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(MessageBody);
bool readSuccess = FJsonSerializer::Deserialize(JsonReader, JsonParsed);
if (readSuccess){
int size = JsonParsed->GetNumberField("size");
if (size > 1)
{
TArray <TSharedPtr<FJsonValue>> array = JsonParsed->GetArrayField("parts");
for (int i = 0; i < size; i++)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, array.Pop()->AsString());
}
}
else if (size == 1){
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, JsonParsed->GetStringField("parts"));
}
}
else {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, MessageBody);
}
The ‘MessageBody’ is a FString having the response output.
It contains the output as follow:
[{“size”:3},{“parts”:[“part1”,“part2”,“part3”]}]
My code always goes in the last ‘else’,i.e., the Deserialize failed(readSuccess == false).
Thanks in advance.
it seems like your JSON string is not correct (use e.g. jsonlint.com to validate). Either do not remove the brackets at line 2 and 3 or use this format:
Why are you removing the brackets at the start and end? Your JSON was valid, but you were making it invalid by removing the square brackets at the start and the end before parsing.
What do you mean didn’t work? There are some other problems with your code if you leave in the square brackets, but readSuccess should be true.
Your original string is valid JSON: You are returning an array with two elements. The first element is an object with a “size” field, and the second object contains the field “parts” which points to another array.
You could make your entire JSON response a lot simpler by getting rid of “size”, which isn’t necessary since JSON parsers know the size of the array.
If you just need an array of strings, then instead of:
[{“size”:3},{“parts”:[“part1”,“part2”,“part3”]}]
You could just have:
[“part1”,“part2”,“part3”]
Then do:
for (int i = 0; i < array.Num(); i++)
If you wanted additional data other than parts, you could make it an object:
{“otherData”:“blah”, “parts”:[“part1”,“part2”,“part3”]}
If you wanted multiple “sections”, you could make an array of those objects:
[{“otherData”:“blah”, “parts”:[“part1”,“part2”,“part3”]}, …]
Hope some of this information is helpful. A multi-dimensional array in JSON is fairly simple, it’s just something like this:
[[“00”,“01”],[“10”,“11”]]
Which would give you an array like this:
a[0][0] = “00”;
a[0][1] = “01”;
a[1][0] = “10”;
a[1][1] = “11”;
that ‘size’ data is merely something for testing purpose. I am sending some other data in future.
readSuccess wasn’t true. I tried it. I didn’t use the lines where I am removing the square brackets.(Commented those)
But it still didn’t work. I did try. A lot of times.