2

I'm trying to parse a JSON file that I created from a JS app into Unity, in C#.

I have no experience with Json or reading data in general so I'm sure I'm missing something basic.

Here is how the data structure looks like in JS:

function DialIn () {
    this.id = null; this.line = "default in line"; this.links = [];
}

function DialOut () {
    this.id = null; this.line = "defauLt out line"; this.links = []; this.autoDelete = false;
}

function DialNode () {
    this.id = null; this.posX = null; this.posY = null; this.ins = []; this.outs = [];
}
            
var NodesDB = []; //DataBase of dial nodes

In a nutshell, NodesDB is an array of DialNode and each DialNode contains an array of ins and an array of outs.

Once store in a JSON, it looks like something like this (for example, with 3 nodes in NodesDB)

[{"id":"dn0","posX":82,"posY":234,"ins":[{"id":"dn0_in_0","line":"This is dynamic Blep ?","links":[]}],"outs":[{"id":"dn0_out_0","line":"Coucou, this is a reply.","links":["dn1_in_0"],"autoDelete":false}]},{"id":"dn1","posX":520,"posY":171,"ins":[{"id":"dn1_in_0","line":"This is dynamic Blep ?","links":["dn0_out_0"]}],"outs":[{"id":"dn1_out_0","line":"Coucou, this is a reply.","links":[],"autoDelete":false},{"id":"dn1_out_1","line":"Coucou, this is a reply.","links":["dn2_in_0"],"autoDelete":false}]},{"id":"dn2","posX":948,"posY":139,"ins":[{"id":"dn2_in_0","line":"This is dynamic Blep ?","links":["dn1_out_1"]}],"outs":[{"id":"dn2_out_0","line":"Coucou, this is a reply.","links":[],"autoDelete":false}]}]

Then, in Unity, I created this structure, trying to mimic the structures from JS:

    [System.Serializable]
    class DialIn
    {
        string id; string line; string[] links;
    }


    [System.Serializable]
    class DialOut
    {
        string id; string line; string[] links; bool autoDelete;
    }


    [System.Serializable]
    class DialNode
    {
        string id; int posX, posY; DialIn[] ins; DialOut[] outs;
    }

    DialNode[] NodesDB;

From there, I'm using these commands:

path = Application.dataPath + "/Jsons/Bloup.json";
jsonData = File.ReadAllText(path);

And I believe I should use something like this: NodesDB = JsonUtility.FromJson<DialNode>(jsonData);

But apparently, it's not that simple.

Any help would be greatly appreciated :o

1
  • I have created a much simpler version of my script without the arrays of arrays and all that noise. The FromJson is working well in that case. I think the problem is that my Json doesn't contain a class but an array of said class. I haven't found a workaround,though. I'm looking into this: answers.unity.com/questions/1123326/… Commented Oct 18, 2020 at 15:40

2 Answers 2

4

Make all your object's member variables public. JSONUtility doesn't (de)serialize private member variables.
See the documentation.

Alternatively if you really want your member variables private, add the [SerializeField] attribute to every field you want serialized.

Sign up to request clarification or add additional context in comments.

4 Comments

I've made all of my classes and NodesDB public but there is still a few things wrong. This is where I want to put my data: public DialNode[] NodesDB; But when I use the command NodesDB = JsonUtility.FromJson<DialNode>(nodesData); I get an error "Cannot implicitly convert type DialNode to DialNode[]" I don't see how to deal with the array, but I'm not even sure I get the syntax right, with the FromJson.
I can work around the error using NodesDB = JsonUtility.FromJson<DialNode[]>(nodesData); but then, I get: "ArgumentException: JSON must represent an object type."
I guess you need another class to wrap around your nodes array, and your json should not start with an array as the root, but should look like this: { "nodesDB":[{...},{...},etc]}. Does that help?
It does! I've been reading that Unity's parser is not supporting arrays at the root of the json file. I guess once the data is extracted, I can get rid of this 'container' and there we go =)
1

you can add simple json to your project and them using on your top script, I use this Script for parse and other...

 var N = JSON.Parse(the_JSON_string);
 var versionString = N[0]["id"].Value;  
 var versionString = N[0]["posX"].Value;  

and other ....

Comments

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.