UE4 構造Json

先記錄:

格式一:數組嵌套

 

實現

 1 FString GenerateJson(const FString Msg)
 2 {
 3     TSharedPtr<FJsonObject> RootJsonObj = MakeShareable<FJsonObject>(new FJsonObject);
 4     RootJsonObj->SetStringField(TEXT("prompt"), Msg);
 5 
 6     TArray<TSharedPtr<FJsonValue>> HistoryJson;
 7     {
 8         TSharedPtr<FJsonObject> TempObj = MakeShareable(new FJsonObject());
 9         for (auto Value : PromptHistory)
10         {
11             TArray<TSharedPtr<FJsonValue>> TempJson;

          TempJson.Add(MakeShareable(new FJsonValueString(Value.Prompt)));
          TempJson.Add(MakeShareable(new FJsonValueString(Value.Response)));

13             HistoryJson.Add(MakeShareable(new FJsonValueArray(TempJson)));
14         }
15 
16     }
17     RootJsonObj->SetArrayField(TEXT("history"), HistoryJson);    
18 
19     FString JsonStr;
20     TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&JsonStr);
21     FJsonSerializer::Serialize(RootJsonObj.ToSharedRef(), Writer);
22 
23     UE_LOG(LogTemp, Error, TEXT("---------%s"), *JsonStr);
24 
25     return JsonStr;
26 }

 

格式二:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章