2

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.

2
  • are you interested in all the contents in this JSON response, or you just want some specific string(s)? Commented Oct 25, 2014 at 9:19
  • 1
    How is this json different? I see objects and arrays and validates nice on jsonlint.com What is your problem? Commented Oct 25, 2014 at 9:25

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It's the first time I use JSON and I couldn't figure it out by following stuff online

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.