Instead of using JSONObject you may use JSONArray. If you really need to convert it to a List you may do something like:
List<JSONObject> list = new ArrayList<JSONObject>();
try {
int i;
JSONArray array = new JSONArray(string);
for (i = 0; i < array.length(); i++)
list.add(array.getJSONObject(i);
} catch (JSONException e) {
System.out.println(e.getMessage());
}
Answer from Victor Dodon on Stack OverflowInstead of using JSONObject you may use JSONArray. If you really need to convert it to a List you may do something like:
List<JSONObject> list = new ArrayList<JSONObject>();
try {
int i;
JSONArray array = new JSONArray(string);
for (i = 0; i < array.length(); i++)
list.add(array.getJSONObject(i);
} catch (JSONException e) {
System.out.println(e.getMessage());
}
There is an answer of your question: https://stackoverflow.com/a/17037364/1979882
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonObject;
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.get(i).toString());
}
}
java - converting JSONObject to Object list - Stack Overflow
Casting list of JSONObject to JSONObject throws ClassCastException
Convert Single Json Object to a List
java - JSON: Get list of JSON Objects - Stack Overflow
Videos
The original "list" is not well-formed JSON. Perhaps you meant the following:
[{"a":1, "b":2}, {"c":3, "d":4}, {"e":5, "f":6}]
If it is in a file called testFile.json, you can import it as JSON:
jsonLst = Import[FileNameJoin[{"path", "to", "testFile.json"}], "JSON"];
If the snippet is a string called str, you can import it as follows:
jsonLst = ImportString[str, "JSON"];
In either case, you will have a list of lists of rules:
(* jsonLst = {{"a" -> 1, "b" -> 2}, {"c" -> 3, "d" -> 4}, {"e" -> 5, "f" -> 6}} *)
You can now convert this into the structure you desire. One of the several ways to do it is the following:
res = jsonLst // Nest[Map, Apply[List], 2];
(* {{{"a", 1}, {"b", 2}}, {{"c", 3}, {"d", 4}}, {{"e", 5}, {"f", 6}}} *)
EDIT
Following the suggestion by Mr. Wizard, this is indeed more general:
res = jsonLst // ReplaceAll[Rule -> List]
My intention was to actually give a more restricted solution so as to avoid unintended side-effects.
EDIT 2
And following the suggestion by b3m2a1, here is his recommended solution:
res = jsonLst // Replace[#, Rule -> List, {3}, Heads -> True] &
If you like, you can call the JSONTools on the string directly:
json = "[{\"a\":1, \"b\":2}, {\"c\":3, \"d\":4}, {\"e\":5, \"f\":6}]";
JSONTools`FromJSON[json]
(* {
{"a" -> 1, "b" -> 2},
{"c" -> 3, "d" -> 4},
{"e" -> 5, "f" -> 6}
} *)
Your root JSON is an Array, so first create a JSONArray from your String.
Do this:
JSONArray arr = new JSONArray(jstring);
for (int i = 0; i < arr.length(); i++) { // Walk through the Array.
JSONObject obj = arr.getJSONObject(i);
JSONArray arr2 = obj.getJSONArray("fileName");
// Do whatever.
}
For more info, please refer to the docs on JSONArray and JSONObject.
You have to directly construct JSONArray from JSON string in this case.
JSONArray arr = new JSONArray(jstring);
you can get this data as a JsonArray
You can customize a little bit of code like it
public static void main(String[] args) throws ParseException {
String data = "{\"data\":[\"str1\", \"str2\", \"str3\"]}";
JSONObject json = new JSONObject(
data);
JSONArray jasonArray = json.getJSONArray("data");
List list = new ArrayList();
int size = jasonArray.length();
int i = 0;
while (i < size) {
list.add(jasonArray.get(i));
i++;
}
System.out.println(list);
}
Hi, All,
I am facing problem while parsing json tried different ways to parse json ending up with error. I actually want to get the size of list present in jsonObject so that I can run a for loop till that size and get attributes accordingly.
Running to the error - org.json.JSONException : A JSONArray text must start with '[' at 1 [character 2 line 1]
Json is present on the below link :
https://reqres.in/api/users?page=2
Can you guys please help me with different code snippet how can it be achieved? Will be very thankful to you all.
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.
For example, to get the property "value" from a List<SomeClass> where SomeClass has a String getValue() and setValue(String value):
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"));
Let us assume that the class is Data with two objects name and dob which are both strings.
Initially, check if the list is empty. Then, add the objects from the list to a 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.
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();