x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to retrieve single text field from Json?

Greetings all,

Currently I have got a json file from the Met Office printed in its entirety to the screen as a string but I wish to be able to print selected values from the json to the screen. As the json is nested I have constructed USTRUCTS to store the converted values to. For example:

 USTRUCT()
 struct FJsonData
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY()
     FSiteRep SiteRep;
 
     UPROPERTY()
     FWx Wx;
 
     UPROPERTY()
     TArray<FParam> Param;
 
     UPROPERTY()
     FDV DV;
 
     UPROPERTY()
     FLocation Location;
 
     UPROPERTY()
     TArray<FPeriod> Period;
 
     UPROPERTY()
     FRep Rep;
 };

This is the main USTRUCT which contains the other USTRUCTs. The other USTRUCTs contain FString variables to store the values from the JsonString.

My problem lies in that my UStructs are empty and I am uncertain how to populate them with the relevant values from the JSonObject. Here is my method so far.

 void AHttpActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
 {
     //Create a pointer to hold the json serialized data
     TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
     
     //Store Json as string
     FString JsonString  = Response->GetContentAsString();
     
     //Create a reader pointer to read the json data
     TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(JsonString);
 
     //Deserialize the json data given Reader and the actual object to deserialize
     if (FJsonSerializer::Deserialize(Reader, JsonObject))
     {
         //Convert JsonObjectString to UStruct
         FJsonData JsonData;
         FJsonObjectConverter::JsonObjectStringToUStruct<FJsonData>(
             JsonString,
             &JsonData,
             0, 0);
         
         //Get the value of the json object by field name
         JsonData.Location.country = JsonObject->GetStringField(TEXT("country"));
 
         //Outputs Json as a string
         GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Yellow, FString(JsonString));
         //Outputs nothing
         GEngine->AddOnScreenDebugMessage(5, 5.0f, FColor::Green, FString(JsonData.Location.country));
     }
     else
     {
         GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Red, FString("error"));
     }
 }

Hit a blocker with this and was hoping for some assistance; any help would be appreciated.

Product Version: UE 4.10
Tags:
more ▼

asked Mar 17 '16 at 05:53 PM in C++ Programming

avatar image

GraeChan
26 2 2 4

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

Ok first things first forget about the USTRUCTS and FJsonObjectConverter for now.

Here is my simple solution:

     //Declare json as string variable
     FString JsonString = Response->GetContentAsString();
     //Create a pointer to hold the json serialized data
     TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
     //Create a reader pointer to read the json data
     TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);
 
     //Deserialize the json data given Reader and the actual object to deserialize
     if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
     {
         TSharedPtr<FJsonObject> objSiteRep = JsonObject->GetObjectField("SiteRep");
         TSharedPtr<FJsonObject> objDV = objSiteRep->GetObjectField("DV");
         TSharedPtr<FJsonObject> objLocation = objDV->GetObjectField("Location");
         FString placeName = objLocation->GetStringField("name");
         FString country = objLocation->GetStringField("country");
 
         //Outputs place name as a string in engine
         GEngine->AddOnScreenDebugMessage(5, 5.0f, FColor::Green, FString(name));
         //Outputs country as a string in engine
         GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, FString(country));
     }
     else
     {
         GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Red, FString("error, failed to deserialize"));
     }

It is far from elegant but it works for the little data I am using it for. The object arrays were more fun but I will not clutter up this space with them unless someone asks.

Hope this helps someone.

more ▼

answered Mar 19 '16 at 04:07 PM

avatar image

GraeChan
26 2 2 4

(comments are locked)
10|2000 characters needed characters left

I've added quite a bit of details to this question: https://answers.unrealengine.com/questions/81533/how-do-i-perform-basic-json-operations-loop-proper.html

It gives an overview and simple example from start to finish on how to work with Json. Maybe it will solve your problem.

more ▼

answered Mar 18 '16 at 09:52 AM

avatar image

staticvoidlol
2.6k 95 40 111

avatar image GraeChan Mar 19 '16 at 03:48 PM

Thanks for the reply, although it wasn't what I was looking for it encouraged me to keep going. My question was a little bit unspecific and it turned out my main problem was that I couldn't access the nested objects within the JSON. I will post my solution in the answer box in case someone else comes looking.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question