UE4 读Json并保存和读取


USTRUCT(BlueprintType)
struct FPlaceInfo
{
    GENERATED_BODY()
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Struct)
        int Id;
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Struct)
        FString Name;
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Struct)
        FTransform Transform;
};
void UMapBlueprintFunctionLibrary::ExportJson(TArray<FPlaceInfo> PlaceInfoArray)
{

    FString JsonTxt;
    TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&JsonTxt);
    Writer->WriteObjectStart();
    Writer->WriteIdentifierPrefix("GameObject");
    Writer->WriteArrayStart();
    for (int i = 0; i < PlaceInfoArray.Num(); ++i)
    {
        //AActor* Actor = ActorArray[i];
        Writer->WriteObjectStart();

        FPlaceInfo Info = PlaceInfoArray[i];
        //FString Name = Actor->GetFName().ToString();
        Writer->WriteValue("Name", Info.Name);

        //FString Tag = Actor->Tags[0].ToString();
        Writer->WriteValue("Id",Info.Id);

        Writer->WriteIdentifierPrefix("Location");
        //Writer->WriteArrayStart();
        Writer->WriteObjectStart();
        Writer->WriteValue("X",Info.Transform.GetLocation().X);
        Writer->WriteValue("Y", Info.Transform.GetLocation().Y);
        Writer->WriteValue("Z",Info.Transform.GetLocation().Z);
        Writer->WriteObjectEnd();
        //Writer->WriteArrayEnd();

        Writer->WriteIdentifierPrefix("Rotation");
        //Writer->WriteArrayStart();
        Writer->WriteObjectStart();
        Writer->WriteValue("X", Info.Transform.GetRotation().Euler().X);
        Writer->WriteValue("Y", Info.Transform.GetRotation().Euler().Y);
        Writer->WriteValue("Z", Info.Transform.GetRotation().Euler().Z);
        Writer->WriteObjectEnd();
        //Writer->WriteArrayEnd();
        

        Writer->WriteIdentifierPrefix("Scale");
        //Writer->WriteArrayStart();
        Writer->WriteObjectStart();
        Writer->WriteValue("X", Info.Transform.GetScale3D().X);
        Writer->WriteValue("Y", Info.Transform.GetScale3D().Y);
        Writer->WriteValue("Z", Info.Transform.GetScale3D().Z);
        Writer->WriteObjectEnd();
        //Writer->WriteArrayEnd();

        Writer->WriteObjectEnd();

    }
    
    Writer->WriteArrayEnd();
    Writer->WriteObjectEnd();
    Writer->Close();


    IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();

    const void* ParentWindowWindowHandle = FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr);
    //const FText Title = FText::Format(LOCTEXT("CurveTable_ExportJSONDialogTitle", "Export '{0}' as JSON..."), FText::FromString("New"));
    const FText Title = LOCTEXT("CurveTable_ExportJSONDialogTitle", "Export Json...");
    const FString CurrentFilename = "NewMap";//CurTable->AssetImportData->GetFirstFilename();
    const FString FileTypes = TEXT("Curve Table JSON (*.json)|*.json");

    TArray<FString> OutFilenames;
    DesktopPlatform->SaveFileDialog(
        ParentWindowWindowHandle,
        Title.ToString(),
        (CurrentFilename.IsEmpty()) ? TEXT("") : FPaths::GetPath(CurrentFilename),
        (CurrentFilename.IsEmpty()) ? TEXT("") : FPaths::GetBaseFilename(CurrentFilename) + TEXT(".json"),
        FileTypes,
        EFileDialogFlags::None,
        OutFilenames
    );

    if (OutFilenames.Num() > 0)
    {
        FFileHelper::SaveStringToFile(JsonTxt, *OutFilenames[0]);
    }
}

TArray<FPlaceInfo> UMapBlueprintFunctionLibrary::ImportJson()
{
    TArray<FPlaceInfo> PlaceInfoArray;
    FString FileTypes, AllExtensions;
    FileTypes = TEXT("Curve Table JSON (*.json)|*.json");//FString::Printf(TEXT("All Files (%s)|%s|%s"), *AllExtensions, *AllExtensions, *FileTypes);

    // Prompt the user for the filenames
    TArray<FString> OpenFilenames;
    IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
    bool bOpened = false;
    int32 FilterIndex = -1;

    if (DesktopPlatform)
    {
        const void* ParentWindowWindowHandle = FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr);

        bOpened = DesktopPlatform->OpenFileDialog(
            ParentWindowWindowHandle,
            LOCTEXT("ImportDialogTitle", "Import").ToString(),
            "",//FEditorDirectories::Get().GetLastDirectory(ELastDirectory::GENERIC_IMPORT),
            TEXT(""),
            FileTypes,
            EFileDialogFlags::Multiple,
            OpenFilenames,
            FilterIndex
        );
    }
    bool bLoad = false;
    FString JsonTxt;
    if (bOpened)
    {
        bLoad = FFileHelper::LoadFileToString(JsonTxt, *OpenFilenames[0]);
        //GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, JsonTxt);
    }
    if (bLoad)
    {
        
        TSharedPtr<FJsonObject> JsonObject;
        TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonTxt);

        if (FJsonSerializer::Deserialize(Reader,JsonObject))
        {
            const TArray<TSharedPtr<FJsonValue>> Files = JsonObject->GetArrayField("GameObject");

            for (int i=0;i<Files.Num();++i)
            {
                
                FString Name;
                int Id;
                FVector Location;
                FVector Rotation;
                FVector Scale;
                const TSharedPtr<FJsonObject>* FileMessageObject;
                if (Files[i].Get()->TryGetObject(FileMessageObject))
                {
                    Name = FileMessageObject->Get()->GetStringField("Name");
                    Id = FileMessageObject->Get()->GetIntegerField("Id");
                    /*const TSharedPtr<FJsonObject>* LocationObject;
                    const TArray<TSharedPtr<FJsonValue>> LocationFile = FileMessageObject->Get()->GetArrayField("Location");
                    for (int j=0;j<LocationFile.Num();++j)
                    {
                        if (LocationFile[j].Get()->TryGetObject(LocationObject))
                        {
                            Location.X = LocationObject->Get()->GetIntegerField("X");
                            Location.Y = FileMessageObject->Get()->GetObjectField("Location")->GetIntegerField("Y");
                            Location.Z = FileMessageObject->Get()->GetObjectField("Location")->GetIntegerField("Z");
                        }
                    }*/
                    
                    
                    Location.X = FileMessageObject->Get()->GetObjectField("Location")->GetIntegerField("X");
                    Location.Y = FileMessageObject->Get()->GetObjectField("Location")->GetIntegerField("Y");
                    Location.Z = FileMessageObject->Get()->GetObjectField("Location")->GetIntegerField("Z");

                    Rotation.X = FileMessageObject->Get()->GetObjectField("Rotation")->GetIntegerField("X");
                    Rotation.Y = FileMessageObject->Get()->GetObjectField("Rotation")->GetIntegerField("Y");
                    Rotation.Z = FileMessageObject->Get()->GetObjectField("Rotation")->GetIntegerField("Z");

                    Scale.X = FileMessageObject->Get()->GetObjectField("Scale")->GetIntegerField("X");
                    Scale.Y = FileMessageObject->Get()->GetObjectField("Scale")->GetIntegerField("Y");
                    Scale.Z = FileMessageObject->Get()->GetObjectField("Scale")->GetIntegerField("Z");
                    
                    FPlaceInfo Info;
                    Info.Id = Id;
                    Info.Transform.SetLocation(Location);
                    Info.Transform.SetRotation(FQuat::MakeFromEuler(Rotation));
                    Info.Transform.SetScale3D(Scale);
                    PlaceInfoArray.Add(Info);
                    //FString LocStr = FString::Printf(TEXT("Location (%s)|%s|%s"),*FString::SanitizeFloat( Location.X), *FString::SanitizeFloat(Location.Y), *FString::SanitizeFloat(Location.Z));
                    //FString RotStr = FString::Printf(TEXT("Rotation (%s)|%s|%s"), *FString::SanitizeFloat(Rotation.X), *FString::SanitizeFloat(Rotation.Y), *FString::SanitizeFloat(Rotation.Z));
                    //FString ScaleStr = FString::Printf(TEXT("Scale (%s)|%s|%s"), *FString::SanitizeFloat(Scale.X), *FString::SanitizeFloat(Scale.Y), *FString::SanitizeFloat(Scale.Z));
                    //GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, LocStr);
                    //GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, RotStr);
                    //GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, ScaleStr);
                    //GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, Name);
                }
            }
        }
        
    }

    return PlaceInfoArray;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM