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 }
格式二: