在C#中解析笨拙的JSON格式

Den*_*nis 2 c# json

使用DSML在我的大学开发的场景建模场景.现在可以将其导出为JSON格式,示例如下所示:

{
    "_entries": [
        "g0"
    ],
    "_flow": {
        "g3": {
            "_expr": [
                {
                    "word_": "player",
                    "next_": [
                        {
                            "word_": "rebukes",
                            "next_": [
                                {
                                    "word_": "jack"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "g4": {
            "_expr": [
                {
                    "word_": "player",
                    "next_": [
                        {
                            "word_": "supports",
                            "next_": [
                                {
                                    "word_": "jack"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "g2": {
            "_type": "cho",
            "_paths": [
                "g3",
                "g4"
            ]
        },
        "g1": {
            "_next": "g2",
            "_expr": [
                {
                    "word_": "player",
                    "next_": [
                        {
                            "word_": "goes to",
                            "next_": [
                                {
                                    "word_": "jack"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "g0": {
            "_next": "g1",
            "_expr": [
                {
                    "word_": "jack",
                    "next_": [
                        {
                            "word_": "bullies",
                            "next_": [
                                {
                                    "word_": "jeff"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以基本上可以在JSON中声明多个流.(每个条目都指向一个新流程的开头,只需看一下它,它就很容易理解和理解).

现在我将导入此JSON文件放在Unity3D我要解析它的位置C#,并将游戏基于此JSON文件中声明的内容.我很擅长使用Unity,C#和JSON,这种JSON格式与大多数教程解释的完全不同.我无法理解这一个.

Dar*_*rov 5

您可以使用以下数据结构对此进行建模:

[DataContract]
public class Data
{
    [DataMember(Name = "_entries")]
    public string[] Entries { get; set; }

    [DataMember(Name = "_flow")]
    public IDictionary<string, Flow> Flow { get; set; }
}

[DataContract]
public class Flow
{
    [DataMember(Name = "_expr")]
    public Expression[] Expressions { get; set; }
}

[DataContract]
public class Expression
{
    [DataMember(Name = "word_")]
    public string Word { get; set; }

    [DataMember(Name = "next_")]
    public Expression[] Next { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后使用JSON.NETJSON字符串轻松反序列化到此结构:

string json = ...
Data data = JsonConvert.DeserializeObject<Data>(json);
Run Code Online (Sandbox Code Playgroud)