Storing Json data on a list of lists

Hello guys, I’m building a simulation,

I get some strings from a JSON file, then I do a search on another JSON for those same strings, and then i get the coordinates I need from the corresponding strings, so far so good. I then store those values on a Vector2 List which is inside a list of lists, that i can see in the inspector, but when I hit play the foreach(foreach(foreach(if)))) that gets the values from the JSON’S to the Vector2List is ignoring the first 9 coordinates and just starts to store them from the last coordinate of the first array of strings.

The strings are stored on a JSON inside Arrays. like this Json—> JSON(Mission(elements(string),Mission, Mission, Mission). here is a screenshot of the Json structure and the code I’m using to get the values.

    void Start()
        pathMission = Application.streamingAssetsPath + "/mission.json";
        jsonStringMission = File.ReadAllText(Application.streamingAssetsPath + "/mission.json");

        pathMap = Application.streamingAssetsPath + "/map.json";
        jsonStringMap = File.ReadAllText(Application.streamingAssetsPath + "/map.json");

        JSONNode mission = JSON.Parse(jsonStringMission);
        JSONNode map = JSON.Parse(jsonStringMap);

        var elements = mission["Missions"][0]["elements"];
        string id = mission["Missions"][0]["id"].Value;

        elementsString = elements.ToString();

        Debug.Log(elementsString + "AQUIIII");

        foreach (JSONNode mizzione in mission["Missions"])
            Debug.Log("Id da missão: " + mizzione["id"]);
            mizzioni = mizzione["id"];
            //add missions to missions list 
            allMissions.Missions.Add(new Point(tempiV)); 

            foreach (JSONNode elementto in mizzione["elements"])
                Debug.Log("Value do elemento: " + elementto.Value);
                elementti = elementto.Value;

                foreach (JSONNode path in map["layer"]["base"]["shapes"])
                    Debug.Log("Value do pathId: " + path["id"]);
                    pathino = path["id"].Value;

                    if (elementti == pathino)
                        Debug.Log("mesmo Id encontrado: " + pathino);
                        Debug.Log("Este é o x: " + path["positionEnd"]["x"].Value + " Este é o y: " + path["positionEnd"]["y"].Value);

                        Vector2 tempV;
                        tempV.x = float.Parse(path["positionEnd"]["x"].Value);
                        tempV.y = float.Parse(path["positionEnd"]["y"].Value);
                        tempiV = tempV;


                        int index = allMissions.Missions.Count - 1;
                        //allMissions.Missions[index].Waypoints = new List<Vector2>(); if I uncomment this line the values are added but they don't start from the first element, if i leave it like this the first and correct value is passed to tempiV 

This is one mission from the missions JSON there are 3 more elements on this same array:

      "id": "14bf1950-089e-4eda-9fb0-349364212359",
      "name": "B -> SD2",
      "elements": [
        "b52f712f-0a2a-4de4-9cc9-00b205718489" this is the first value being added
      "source": "d1165857-bd4e-4f7c-aede-b20d7d33cdba",
      "destination": "a3b17cdf-13f1-4130-8fad-580d195ac7da",
      "flow": 0,
      "color": "#8ac1ff",
      "missionErrors": {
        "missingFlow": true,
        "missingPath": false
      "$$hashKey": "object:3873"

And this is the other JSONwhich i get the coordinates from, this is the first element jus tto give an idea:

  "layer": {
    "base": {
      "shapes": [
          "type": "S:S",
          "display": {},
          "customName": "B",
          "visible": true,
          "locked": false,
          "positionStart": {

            "x": 40,
            "y": 40
          "positionEnd": {
            "x": 140,
            "y": 260
          "id": "d1165857-bd4e-4f7c-aede-b20d7d33cdba",
            "lineNormal": "#ba1026",
            "lineHover": "rgba(186,16,38,0.7)",
            "lineSelected": "#ba1026",
            "fillNormal": "#d8d8d8"

Sorry but your code is very confusing and your provided JSON text does not match what you’re trying to parse. A “shape” in your second json text doesn’t have an “id”. Also the last line:


doesn’t seem to make much sense either. It’s also a bit strange that allMissions.Missions seem to be a list of “Point” instances. You probably should refactor your classnames.

Since you process one “Point” / mission per iteration in the outer loop, it would make more sense to store that new Point in a local variable and use that to add waypoints.

Generally you can simplify most of the parsing by putting the mission specific parsing inside a method inside the Point class and just pass the JSONNode for that mission.

Finally I’d like to note that SimpleJSON has the Unity specific SimpleJSONUnity extension file. If you have that in your project you can read vectors just like this:

tempiV = path["positionEnd"];

which is the same as

tempiV = path["positionEnd"].ReadVector3();

ReadVector3 can read vectors from arrays [1.2, 3.2, 20] or from objects {"x":1.2, "y":3.2, "z": 20}

The extension file comes with several overloads to provide a default value or to pass custom component names. For example


The extension also provides support for other Untiy types (Vector2/3/4, Quaternion, Rect, RectOffset, Matrix4x4)