Dennis
Dennis

Reputation: 3690

Parsing awkward JSON format in C#

A scenario is modelled using a DSML which is developed at my university. Now this can be exported to a JSON format, an example looks like this:

{
    "_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"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

So basically there can be more than one flow declared in the JSON. (every entry points to the start of a new flow, just take a look at it, it's fairly easy to read and understand it).

Now I am importing this JSON file in Unity3D where I want to parse this using C# and base the game on what is declared in this JSON file. I am quite new to using Unity, C# and JSON and this JSON format is completely different than what most tutorials explain. I cannot get my head around this one.

Upvotes: 2

Views: 108

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

You could model this with the following data structure:

[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; }
}

and then using JSON.NET easily deserialize the JSON string to this structure:

string json = ...
Data data = JsonConvert.DeserializeObject<Data>(json);

Upvotes: 5

Related Questions