I have a large JSON file that contains A LOT of similar code. It’s similar to this:
...,"techs":{"t1":{"level":24,"able":true},"t2":{"level":23,"able":true},"t3":{"level":20,"able":true},"t4"...,"t5"...
It has since t1 until t510… For this reason, I have to create an activity for each tN, so I have to create 510 activities! 0.0
To get acces to each tN I use the following lines:
Gson gson = new Gson();
Planets json = gson.fromJson(str, Planets.class);
System.out.println(json.techs.t1.level);
System.out.println(json.techs.t2.level);
etc...
So I wanna know if there’s the possibility to change t1 for a variable, so that I only have to change the variable to access t2 in a single activity.
For example: String tech = t456; System.out.println(json.techs.tech.level);
Thank you very much in advance!
It’s all just about your imagination 😉
I will come out from this JSON snippet
This is easily representable as this structure
where
InnerObjectclass is defined like this:So everything you need is class, where single field will be called
techsand it will be defined like this:To access fields after, you can use:
Whole code:
And you can walk through all items in
HashMaplike this: