先记录:
格式一:数组嵌套
实现
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 }
格式二:
标签:JsonStr,MakeShareable,TEXT,构造,Json,RootJsonObj,new,TempJson,UE4 From: https://www.cnblogs.com/-sev-/p/17350385.html