Reading Json Output

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.

Hi silentkratos,

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:

{
    "size": 3,
    "parts": [
        "part1",
        "part2",
        "part3"
    ]
}

Thanks that worked…

Do you know any link or website where a tutorial can be found for multi-dimensional array in json?

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.

Even if I kept it, it didn’t work…

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”;

Your information is helpful. No doubts. But,

  1. that ‘size’ data is merely something for testing purpose. I am sending some other data in future.
  2. 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.

Oh, it’s because of the way you’re reading it. If you have an array, that technically isn’t a JSON object, so instead of:

TSharedPtr JsonParsed = MakeShareable(new FJsonObject());

You need:

TSharedPtr JsonParsed = MakeShareable(new FJsonValue());

See:

vs.

Formatting was a little messed up, not sure how to format properly in comments.

oh… Thanks man… really appreciated… Will look into it…