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 add an Array of Json Objects to a Json Object?

Hi there,

how is it possible to add an array of Json objects to the main Json Object?

I managed to add a value array:

 // Json object
 TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
 
 // Value array
 TArray< TSharedPtr<FJsonValue> > ValueArray;
 
 // Create two values and add them to the array
 TSharedPtr<FJsonValue> Value1 = MakeShareable(new FJsonValueString("value1"));    
 ValueArray.Add(Value1);    
 TSharedPtr<FJsonValue> Value2 = MakeShareable(new FJsonValueString("value2"));    
 ValueArray.Add(Value2);

 // Add the array to the Json object
 JsonObject->SetArrayField("array", ValueArray);

However I would like to have the array as JsonObjects. That basically the array would be declared as:

 TArray< TSharedPtr  <FJsonObject> > ObjArray;

and I add to the main FJsonObject:

 // create JsonObj array
 TArray< TSharedPtr<FJsonObject> > ObjArray;

 // create a Json object and add a string field
 TSharedPtr<FJsonObject> JsonObj = MakeShareable(new FJsonObject);    
 JsonObj ->SetStringField("Abc", "AbcValue");

 // add the object to the array
 ObjArray.Add(JsonObj);

 // ***THIS does not work***
 JsonObject->SetArrayField("array", ObjArray);

=====EDIT 1 =====

Compile error:

  • error C2664: 'void FJsonObject::SetArrayField(const FString &,const TArray,FDefaultAllocator> &)' : cannot convert argument 2 from 'TArray,FDefaultAllocator>' to 'const TArray,FDefaultAllocator> &'JsonObject->SetArrayField("array", ObjArray);

  • Reason: cannot convert from 'TArray,FDefaultAllocator>' to 'const TArray,FDefaultAllocator>'

  • No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Thanks!

Product Version: UE 4.9
Tags:
more ▼

asked Dec 02 '15 at 12:03 PM in C++ Programming

avatar image

ionbordura
118 7 18 14

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

2 answers: sort voted first

// create Json values array

TArray< TSharedPtr<FJsonValue> > ObjArray;

// create a Json object and add a string field

TSharedPtr< FJsonObject > JsonObj = MakeShareable(new FJsonObject);

JsonObj->SetStringField("Abc", "AbcValue");

// create a Json value object to hold your Json object

TSharedRef< FJsonValueObject > JsonValue = MakeShareable( new FJsonValueObject( JsonObj) );

// add the object to the array

ObjArray.Add(JsonValue);

// THIS should work

AnotherJsonObj->SetArrayField("array", ObjArray);

Modifications for your code to build in bold

more ▼

answered May 06 '16 at 08:53 AM

avatar image

hsarret
56 1 4 5

avatar image ionbordura Jun 13 '16 at 09:40 AM

Hi, thanks for the answer! You missed the brackets when declaring JsonObj as shared pointer. Could you edit that, it might help others:

 TSharedPtr < FJsonObject > JsonObj

Actually the editor removes it, try adding it as Code sample then it appears.

avatar image hsarret Jun 13 '16 at 02:58 PM

Fixed some typos, thanks for letting me know

Glad it helped

avatar image Jayae Jul 13 '16 at 06:02 PM

I'm finding a problem implementing this, isnt it circular?

Reading backwards; you have JsonObj which has an ArrayField of ObjArray. ObjArray contains a pointer to JsonObj, which has a StringField of "Abc", "AbcValue".

I might just be really confused by trying to modify your answer into my own code, but when I try to serialise JsonObj the editor crashes once it reaches 2GB in size ;)

avatar image hsarret Jul 13 '16 at 07:20 PM

I edited to avoid confusion. But there is no problem with code as it was. If you explain your problem we might be able to help you out

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

I haven't used JSon in UE so far and "doesn't work" is a bit unspecific, but maybe you're running into the same problem as this poster: https://answers.unrealengine.com/questions/290630/how-to-const-cast-tarray-of-tsharedptr.html

more ▼

answered Dec 03 '15 at 10:42 AM

avatar image

erinacea
1.7k 68 27 109

(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