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[]
Your input string should be as follows to qualify for a list of SomeObject
{
[{
"k1": 1,
"k2": "v2",
"k3": "v3"
}, {
"k1": 2,
"k2": "v2",
"k3": "v3"
}
]
}
Below code should work ..
JSONObject jsonObject = new JSONObject(jsonString);
SomeObject[] objectsList = gson.fromJson(jsonObject.toString(), SomeObject[].class);
For given JSON String here you can handle this way (add toString()
to SomeObject
for displaying it)...
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class TestClass {
public static void main(String[] args) {
List<SomeObject> list = new LinkedList<SomeObject>();
String jsonString = "{\"obj_1\":{\"k1\":1,\"k2\":\"v2\",\"k3\":\"v3\"},\"obj_2\":{\"k1\":2,\"k2\":\"v2\",\"k3\":\"v3\"}}";
Gson gson = new Gson();
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
// note entry.getKey() will be obj_1, obj_2, etc.
SomeObject item = gson.fromJson(entry.getValue().getAsJsonObject(), SomeObject.class);
list.add(item);
}
System.out.println(list);
}
}
Sample Run
[SomeObject [k1=1, k2=v2, k3=v3], SomeObject [k1=2, k2=v2, k3=v3]]
NOTE: the value of k1 must be a number and without quotes as k1 is declared as int in SomeObject
您的JSON结构是一个Map,如果您想要数组或列表使用[{},{}],则它是JSON中用于数组或列表的结构
1.You should make your class like this for getter and setter:
public class SomeObject {
public int k1;
public String k2;
public String k3;
public int getK1() {
return k1;
}
public String getK2() {
return k2;
}
public String getK3() {
return k3;
}
public void setK1(int k1) {
this.k1 = k1;
}
public void setK2(String k2) {
this.k2 = k2;
}
public void setK3(String k3) {
this.k3 = k3;
}
}
You can easily convert JSON string to JSONObject.After that make modal objecgt and set value to variables of modal object using following code:
try {
JSONObject jsonObjectData = new JSONObject(YOUR_JSON_STRING);
SomeObject object = new SomeObject();
object.setK1(1);
object.setK2("test 1");
object.setK3("test 2");
List<SomeObject> objectList = new ArrayList<>();
objectList.add(object);
}catch (JSONException e){
e.printStackTrace();
}
The JSON you have posted seems to be like the dynamic type ie you don't have a fixed key for that you can't simply make-array. you have to traverse it manually.
do something like this if you are using GSON.
public ArrayList getArray(String json){
ArrayList<SomeObject> list = new ArrayList<>();
Gson gson = new Gson();
JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
for(Map.Entry<String,JsonElement> entry:obj.entrySet()){
//parse json
SomeObject some = gson.fromJson(entry.getValue().getAsJsonObject(), SomeObject.class);
//here add to list
list.add(some);
}
return list;
}
Note: sample code not tested.
Why not create an ArrayList of JSONObjects?
ArrayList <JSONObject> objectsList = new ArrayList<JSONObject>();
objectsList .add(jsonObject);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.