I have a simple JSON string in the format of:
{
"obj_1": {
"k1": "v1",
"k2": "v2",
"k3": "v3"
},
"obj_2": {
"k1": "v1",
"k2": "v2",
"k3": "v3"
}
}
And a class to represent each JSON object like so:
public class SomeObject {
public int k1;
public String k2;
public String k3;
public Constructor(int k1, String k2, String k3) {
this.k1 = k1;
this.k2 = k2;
this.k3 = k3;
}
I want to read the JSON object into an array of those objects without having to loop using while loops as this takes a really long time.
JSONObject jsonObject = new JSONObject(jsonString);
SomeObject[] objectsList = gson.fromJson(jsonObject.toString(), SomeObject[].class);
The main idea is to populate each JSON object into the Java Class then into an array of SomeObject. This way inside the array i will have access to each object and access the properties using methods.
I end up getting an error message:
can't make objects of type SomeObject[]
SomeObject[] objectsList = gson.fromJson(jsonObject.toString(), SomeObject[].class)toSomeObject[] objectsList = gson.fromJson(jsonObject.toString(), SomeObject.class)Map<String, SomeObject>