Announcement

Collapse
No announcement yet.

Best Way to Handle JSON

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Best Way to Handle JSON

    Hi,

    I'm quite new to c++ in Unreal 4 and I'm currently getting JSON trough REST. I have the response from server but the problem is to go get values properly from json. One thing which confuses me is JsonObject. So how to initialize it properly? How can I set my JSON response to JsonObject and how to get values out of it?

    I'm trying out something like this.

    Code:
    		MessageBody = Response->GetContentAsString();
    		
    		TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
    		TSharedRef< TJsonReader<> > JsonReader = TJsonReaderFactory<>::Create(*MessageBody);
    
    	if (FJsonSerializer::Deserialize(JsonReader, JsonObject) &&
    			JsonObject.IsValid())
    		{
    
    		TArray<TSharedPtr<FJsonValue>> objArray = JsonObject->GetArrayField(TEXT("rows"));
    
    		for (int32 i = 0; i < objArray.Num(); i++)
    		{
    			TSharedPtr<FJsonValue> value = objArray[i];
    			TSharedPtr<FJsonObject> json = value->AsObject();
    
    			FString name = json->GetStringField(TEXT("51205"));
    			
    		}
    Last edited by jmietola; 09-26-2014, 07:51 AM.

    #2
    You can check https://github.com/ufna/VaRest
    Making games with Unreal Engine https://ufna.dev
    My plugins official channel: https://discord.gg/N92pzqJ

    Comment


      #3
      Awesome! I'll check that out.

      Comment


        #4
        Ok I sat down and took a deep breath. I got the value I was looking for.

        Code:
        		MessageBody = Response->GetContentAsString();
        		TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
        		TSharedRef< TJsonReader<> > JsonReader = TJsonReaderFactory<>::Create(MessageBody);
        
        		// Deserialize the JSON data
        
        		if (FJsonSerializer::Deserialize(JsonReader, JsonObject) &&
        			JsonObject.IsValid())
        		{
        
        			TSharedPtr<FJsonObject> jsonObj = JsonObject->GetObjectField("table");
        			TArray<TSharedPtr<FJsonValue>> objArray = jsonObj->GetArrayField("rows");
        
        			for (int32 i = 0; i < objArray.Num(); i++)
        			{
        	
        				TArray<TSharedPtr<FJsonValue>> height = objArray[i]->AsArray();
        				FString name = height[2]->AsString();
        								
        				UE_LOG(LogTemp, Warning, TEXT("Value I'm looking for %s"), *name);
        
        			}
        		}
        JSON:

        Code:
        {
            "table": {
                "columnNames": [
                    "actor",
                    "height"
                ],
                "columnTypes": [
                    "String",
                    "float"
                ],
                "columnUnits": [
                    null,
                    "m"
                ],
                "rows": [
                    [
                        "11111",
                        2.5
                    ],
                    [
                        "11111",
                        2.4
                    ]
                ]
            }
        }
        Last edited by jmietola; 09-27-2014, 10:02 PM.

        Comment

        Working...
        X