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;
 
        //Debug.Log(missions);

        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;

                        //Debug.Log();

                        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 
                        allMissions.Missions[0].Waypoints.Add(tempiV);
                       
                        //Debug.Log(index);
                    }
                }
            }
        }
    }
}

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

{
  "Missions": 
  [
    {
      "id": "14bf1950-089e-4eda-9fb0-349364212359",
      "name": "B -> SD2",
      "elements": [
        "70fb677d-a99b-4c4c-97c2-194cd1ee60d8",
        "4fe60df2-d893-4422-a280-77807891c28d",
        "9dfb8647-5964-4a92-8523-3383b8b0145c",
        "1d3f436e-f031-43a2-b7b8-8ed029b713f8",
        "4205c4a0-0791-43f7-957a-c45991ecec4c",
        "46a76c06-1093-427d-94fe-4c04c4291b39",
        "e3fe50a2-a2ac-490b-91ff-113b05fa92a1",
        "dfc0892b-1ef4-4b9f-8e99-0d01de1ba34d",
        "47cc31e2-d144-4553-8102-a9daa868cb92",
        "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",
          "color": 
          {
            "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:

allMissions.Missions[0].Waypoints.Add(tempiV);

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

someNode.ReadVector3("r","g","b");

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