moawwad
moawwad

Reputation: 51

How to make a List of Lists based off a Json file

I have a JSON file I'm iterating over, using gson, I loop through the file and store them all in lists. The problem I am facing is when I iterate over the nested list. Instead of making the output have multiple lists inside the outer list like this [[], [], []] it is just adding everything into the first inner list like this[[adds everything here]] I'm not sure why it is doing this, I have tried multiple things already nut cant get it to work properly

this is my JSON file I am trying to make a list of lists in which each contain a P, I, and D

{
    "pos":[
       {
         "x": 1.0,
         "y": 1.0,
         "theta": 1.0,
         "drive":[{
            "p": 0.1,
            "i": 0.1,
            "d": 0.1
         }]
       },
       {
         "x": 2.0,
         "y": 2.0,
         "theta": 2.0,
         "drive":[{
            "p": 0.2,
            "i": 0.2,
            "d": 0.2
         }]
          
       },
       {
         "x": 3.0,
         "y": 3.0,
         "theta": 3.0,
         "drive":[{
            "p": 0.3,
            "i": 0.3,
            "d": 0.3
         }]
          
       }
    ]
 }
 

this is how I loop through it

        String json = readFileAsString(file);
        System.out.println(json);

        JsonManager manager = new Gson().fromJson(json, new TypeToken<JsonManager>() {}.getType());

        List<PosData> posList = manager.getPositions();
        List<Double> xList = new ArrayList<>();
        List<Double> yList = new ArrayList<>();
        List<Double> thetaList = new ArrayList<>();
        List<Double> drivePID = new ArrayList<>();
        List<List<Double>> driveLists = new ArrayList<List<Double>>();

        
        for (PosData pos : posList) {
            xList.add(pos.getX());
            yList.add(pos.getY());
            thetaList.add(pos.getTheta());

            for(PidData pid : pos.getDrive()){
                drivePID.add(pid.getP());
                drivePID.add(pid.getI());
                drivePID.add(pid.getD());
                break;
            }

            driveLists.add(drivePID);
            
            System.out.println(drivePID);
            System.out.println(driveLists);
        }

this is the final output I would like to have

[[0.1, 0.1, 0.1], [0.2, 0.2, 0.2], [0.3, 0.3, 0.3]]

but this is what it does when I print out the loop

[[0.1, 0.1, 0.1]]
[[0.1, 0.1, 0.1, 0.2, 0.2, 0.2], [0.1, 0.1, 0.1, 0.2, 0.2, 0.2]]
[[0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3], [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3], [0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3]]

Thanks for any help in advance!

Upvotes: 2

Views: 1087

Answers (1)

lkatiforis
lkatiforis

Reputation: 6255

You have to initialize drivePID list on each loop:

   for (PosData pos : posList) {
        xList.add(pos.getX());
        yList.add(pos.getY());
        thetaList.add(pos.getTheta());

        for (PidData pid : pos.getDrive()) {
          List<Double> drivePID = new ArrayList<>();
          drivePID.add(pid.getP());
          drivePID.add(pid.getI());
          drivePID.add(pid.getD());
          driveLists.add(drivePID);
          break;
        }

        System.out.println(driveLists);
      }

Output:

[[0.1, 0.1, 0.1]]
[[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]
[[0.1, 0.1, 0.1], [0.2, 0.2, 0.2], [0.3, 0.3, 0.3]]

Upvotes: 2

Related Questions