虚幻引擎4中的C ++嵌套JSON [英] C++ Nested JSON in Unreal Engine 4

查看:329
本文介绍了虚幻引擎4中的C ++嵌套JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从服务器获取的JSON对象,看起来像这样:

I have a JSON object that I am getting from my server that looks something like this:

{
    "state":"1",
    "player1": {
        "alias":"Player Name",
        "ready":"0"
    }
}

我能够获取JSON,将其解析为FJsonObject,并使用以下代码进行序列化以检索JSON对象第一级中的任何数字或字符串:

I am able to get the JSON, parse it into a FJsonObject, and retrieve any number or string in the first level of the JSON object using this code to serialize:

TSharedPtr<FJsonObject> JsonParsed;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(json);
if (FJsonSerializer::Deserialize(JsonReader, JsonParsed))
    //Use JsonParsed

此代码读取字符串:

FString AJSONContainer::getStringWithKey(FString key)
{
    return storedJSON->GetStringField(key);
}    


旁注:

AJSONContainer只是一个Actor类,我用它来从Blueprints中调用这些函数.


Side Note:

AJSONContainer is just an Actor class that I use to call these functions from Blueprints.

这一切都很好,但是,当我尝试从第二级阅读内容时,一切都无法正常工作.

That's all fine and dandy, but when I try to read things from the second level, things don't work.

我编写了以下代码以使下一个级别降级:

I wrote this code to get the next level down:

TSharedPtr<FJsonObject> nested = storedJSON->GetObjectField(key);

但是所有获取nested字段的调用都不会返回任何内容.

But all calls to get fields of nested return nothing.

nested->GetStringField(anotherKey); //Nothing

例如,使用上面的JSON,

So, for example, with the above JSON, this:

TSharedPtr<FJsonObject> nested = storedJSON->GetObjectField("player1");
FString alias = nested->GetStringField("alias");

当我将

alias打印到控制台时,它没有任何价值.

alias has no value when I print it to the console.

我做错什么了吗?为什么二级JSON不起作用?

Am I doing something wrong? Why isn't the second-level JSON working?

推荐答案

不知道您是否对它进行了整理,但是我发现了一个非常讨厌的函数,该函数可用于嵌套对象以及数组.而且它为您提供了USTRUCT,因此您不必使用通过键获取值的函数(我不喜欢它们,因为它们很容易出错).相反,您将拥有类型安全性!

Don't know if you got it sorted out, but I found a pretty nasty function that works for nested objects and, also, for arrays altogether. And it gives you a USTRUCT, so you don't have to use the functions that get values by Keys (I don't like them since they're very error prone). Instead, you'll have type safety!

这是 docs ,另外一个问题是 UE4 AnswerHub

Here are the docs and another question answered on UE4 AnswerHub

基本上,您可以创建目标USTRUCT(或嵌套JSON的USTRUCT),并用UPROPERTY标记所有属性,以便Unreal知道其名称,并使用此功能.它将通过匹配配对来复制这些值.它甚至复制阵列! = D

Basically, you create the target USTRUCT (or USTRUCTs for nested JSONs), mark all properties with UPROPERTY, so Unreal knows their names, and use this function. It will copy the values by matchmaking them. It copies even the arrays! =D

我将JSON FString称为反序列化的 Json ,它的结构类似于下面的结构.它包含一个嵌套对象和一个数组,使事情变得有趣.

I'll call the JSON FString to be deserialized Json and it's structure is like the one below. It contains a nested object and an array, to make things interesting.

{
    "nested" : {
        "id" : "654asdf",
        "name" : "The Name"
    },
    "foo" : "foobar",
    "bar_arr" : [
        { "barfoo" : "asdf" },
        { "barfoo" : "qwer" }
    ]
}

在进行转换之前,我们需要从内到外创建USTRUCT(以便可以在外部引用内部).请记住,始终对结构名称使用 F .

Before converting, we need to create the USTRUCTs from inside out (so we can reference inner on the outer). Remember to always use F for struct names.

USTRUCT()
struct FNested
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY()
    FString id;

    UPROPERTY()
    FString name;
};

USTRUCT()
struct FBar
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY()
    FString barfoo;
};

USTRUCT()
struct FJsonData
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY()
    FNested nested;

    UPROPERTY()
    FString foo;

    UPROPERTY()
    TArray<FBar> bar_arr;
};

转换将如下所示:

FJsonData JsonData;
FJsonObjectConverter::JsonObjectStringToUStruct<FJsonData>(
    Json,
    &JsonData,
    0, 0);

现在,您可以像在标准C ++结构中那样访问所有属性.例如,访问 barfoo 之一:

Now, you are able to access all the properties as in standard C++ structs. Eg., to access one of the barfoos:

FString barfoo0 = JsonData.bar_arr[0].barfoo;

我还没有在JSON中使用int和float对其进行测试,但是由于它甚至可以复制数组,因此我相信这也可以.

I have not tested it with int and float in the JSON, but since it copies even arrays, I believe that would work also.

这篇关于虚幻引擎4中的C ++嵌套JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆