I have a List<class>
that I would like to convert into a json object and then traverse the data out of the json object.
我有一个List
If this were just a list<String>
I could just do something like:
如果这只是一个列表
JSONObject obj = new JSONObject();
List<String> sList = new ArrayList<String>();
sList.add("val1");
sList.add("val2");
obj.put("list", sList);
Then I could traverse the list like:
然后我可以遍历列表,如:
JSONArray jArray = obj.getJSONArray("list");
for(int ii=0; ii < jArray.size(); ii++
System.out.println(jArray.getString(ii));
The problem with using the class is that I need to have access to data within each class element of my list<class>
and I don't know how to encode that / traverse it into JSON. Any help would be greatly appreciated.
使用该类的问题是我需要访问列表
5 个解决方案
#1
16
Call getJSONObject()
instead of getString()
. That will give you a handle on the JSON object in the array and then you can get the property off of the object from there.
调用getJSONObject()而不是getString()。这将为您提供数组中JSON对象的句柄,然后您可以从该处获取该对象的属性。
For example, to get the property "value" from a List<SomeClass>
where SomeClass
has a String getValue()
and setValue(String value)
:
例如,要从List
JSONObject obj = new JSONObject();
List<SomeClass> sList = new ArrayList<SomeClass>();
SomeClass obj1 = new SomeClass();
obj1.setValue("val1");
sList.add(obj1);
SomeClass obj2 = new SomeClass();
obj2.setValue("val2");
sList.add(obj2);
obj.put("list", sList);
JSONArray jArray = obj.getJSONArray("list");
for(int ii=0; ii < jArray.length(); ii++)
System.out.println(jArray.getJSONObject(ii).getString("value"));
#2
3
Let us assume that the class is Data with two objects name and dob which are both strings.
让我们假设该类是具有两个对象名称和dob的数据,它们都是字符串。
Initially, check if the list is empty. Then, add the objects from the list to a JSONArray
最初,检查列表是否为空。然后,将列表中的对象添加到JSONArray
JSONArray allDataArray = new JSONArray();
List<Data> sList = new ArrayList<String>();
//if List not empty
if (!(sList.size() ==0)) {
//Loop index size()
for(int index = 0; index < sList.size(); index++) {
JSONObject eachData = new JSONObject();
try {
eachData.put("name", sList.get(index).getName());
eachData.put("dob", sList.get(index).getDob());
} catch (JSONException e) {
e.printStackTrace();
}
allDataArray.put(eachData);
}
} else {
//Do something when sList is empty
}
Finally, add the JSONArray to a JSONObject.
最后,将JSONArray添加到JSONObject。
JSONObject root = new JSONObject();
try {
root.put("data", allDataArray);
} catch (JSONException e) {
e.printStackTrace();
}
You can further get this data as a String too.
您还可以将此数据作为字符串进一步获取。
String jsonString = root.toString();
#3
1
You could use a JSON serializer/deserializer like flexjson to do the conversion for you.
您可以使用像flexjson这样的JSON序列化器/反序列化器来为您进行转换。
#4
1
This is how I do it using Google Gson. I am not sure, if there are a simpler way to do this.( with or without an external library).
这就是我使用Google Gson的方式。我不确定,如果有更简单的方法可以做到这一点。(有或没有外部库)。
Type collectionType = new TypeToken<List<Class>>() {
} // end new
.getType();
String gsonString =
new Gson().toJson(objList, collectionType);
#5
0
Just to update this thread, here is how to add a list (as a json array) into JSONObject. Plz substitute YourClass with your class name;
只是为了更新这个线程,这里是如何将一个列表(作为一个json数组)添加到JSONObject中。 Plz用您的班级名称替换YourClass;
List<YourClass> list = new ArrayList<>();
JSONObject jsonObject = new JSONObject();
org.codehaus.jackson.map.ObjectMapper objectMapper = new
org.codehaus.jackson.map.ObjectMapper();
org.codehaus.jackson.JsonNode listNode = objectMapper.valueToTree(list);
org.json.JSONArray request = new org.json.JSONArray(listNode.toString());
jsonObject.put("list", request);
#1
16
Call getJSONObject()
instead of getString()
. That will give you a handle on the JSON object in the array and then you can get the property off of the object from there.
调用getJSONObject()而不是getString()。这将为您提供数组中JSON对象的句柄,然后您可以从该处获取该对象的属性。
For example, to get the property "value" from a List<SomeClass>
where SomeClass
has a String getValue()
and setValue(String value)
:
例如,要从List
JSONObject obj = new JSONObject();
List<SomeClass> sList = new ArrayList<SomeClass>();
SomeClass obj1 = new SomeClass();
obj1.setValue("val1");
sList.add(obj1);
SomeClass obj2 = new SomeClass();
obj2.setValue("val2");
sList.add(obj2);
obj.put("list", sList);
JSONArray jArray = obj.getJSONArray("list");
for(int ii=0; ii < jArray.length(); ii++)
System.out.println(jArray.getJSONObject(ii).getString("value"));
#2
3
Let us assume that the class is Data with two objects name and dob which are both strings.
让我们假设该类是具有两个对象名称和dob的数据,它们都是字符串。
Initially, check if the list is empty. Then, add the objects from the list to a JSONArray
最初,检查列表是否为空。然后,将列表中的对象添加到JSONArray
JSONArray allDataArray = new JSONArray();
List<Data> sList = new ArrayList<String>();
//if List not empty
if (!(sList.size() ==0)) {
//Loop index size()
for(int index = 0; index < sList.size(); index++) {
JSONObject eachData = new JSONObject();
try {
eachData.put("name", sList.get(index).getName());
eachData.put("dob", sList.get(index).getDob());
} catch (JSONException e) {
e.printStackTrace();
}
allDataArray.put(eachData);
}
} else {
//Do something when sList is empty
}
Finally, add the JSONArray to a JSONObject.
最后,将JSONArray添加到JSONObject。
JSONObject root = new JSONObject();
try {
root.put("data", allDataArray);
} catch (JSONException e) {
e.printStackTrace();
}
You can further get this data as a String too.
您还可以将此数据作为字符串进一步获取。
String jsonString = root.toString();
#3
1
You could use a JSON serializer/deserializer like flexjson to do the conversion for you.
您可以使用像flexjson这样的JSON序列化器/反序列化器来为您进行转换。
#4
1
This is how I do it using Google Gson. I am not sure, if there are a simpler way to do this.( with or without an external library).
这就是我使用Google Gson的方式。我不确定,如果有更简单的方法可以做到这一点。(有或没有外部库)。
Type collectionType = new TypeToken<List<Class>>() {
} // end new
.getType();
String gsonString =
new Gson().toJson(objList, collectionType);
#5
0
Just to update this thread, here is how to add a list (as a json array) into JSONObject. Plz substitute YourClass with your class name;
只是为了更新这个线程,这里是如何将一个列表(作为一个json数组)添加到JSONObject中。 Plz用您的班级名称替换YourClass;
List<YourClass> list = new ArrayList<>();
JSONObject jsonObject = new JSONObject();
org.codehaus.jackson.map.ObjectMapper objectMapper = new
org.codehaus.jackson.map.ObjectMapper();
org.codehaus.jackson.JsonNode listNode = objectMapper.valueToTree(list);
org.json.JSONArray request = new org.json.JSONArray(listNode.toString());
jsonObject.put("list", request);