step 1 : open dilsecodie
step 2: using java Convert Jsonarray to ArrayList
step 3: Pass ArrayList To this method gets Jsonarray in result.
step 4 : Jsonarray can be used to return responses from API
JSONArray can contain: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object; i.e. it is mixed types, making it hard to just dump the elements into a List of some type and sort that,
then pass through the list dumping sorted items back into the JSON array.
the only certain way to get a common type of each element from the
JSONArray is using the Object get() method
of course then all you have is Object objects and won't be able to do any meaningful sorting on them without revisiting the serialization issue.
Assuming your JSONArray contains homogeneously structured values,
you could iterate through the JSONArray, calling one of the typed get() methods on each one,
dumping them into a List type, then sorting on that.
You can simply implement the Interface List and do generics for the type and implement your own class that does the same as ArrayList.
Since you use an array as storage, this should be straight-forward and easy to do.
public class MyArrayList<Type> implements List {
}
Array lists are objects in Java,
which means that they have special features that primitive arrays do not.
It’s a wrapper class, which means that under the hood,
there is primitive array wrapped with the functionality of lists,
which gives the class very powerful methods
public static ArrayList convertJSONArrayToArrayList(JSONObject jsonObject){
JSONArray arrayOfReq = jsonObject.getJSONArray(NetworkConstant.OBJECT_DATA) ;
ArrayList lstJSONArray = new ArrayList();
for(int i = 0; i < arrayOfReq.size() ; i++){
Object objectOnVec = (Object) arrayOfReq.get(i);
if(objectOnVec instanceof JSONObject)
lstJSONArray.add(convertFromJsonObject((JSONObject)objectOnVec));
else
lstJSONArray.add(objectOnVec);
}
return lstJSONArray;
}
public static JSONObject convertListToJSONObject(>
ArrayList requiredListObject) {
JSONObject jsonObjectLst = new JSONObject();
jsonObjectLst.put("DATA",4);
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < requiredListObject.size(); i++) {
Object objectVal = requiredListObject.get(i);
if (objectVal instanceof JSONStructure || objectVal instanceof HashMap
|| objectVal instanceof ArrayList) {
jsonArray.add(convertDSToJSONObject(objectVal));
} else {
if(objectVal instanceof JSONObject) {
jsonArray.add(objectVal);
} else if(objectVal instanceof String || objectVal instanceof Integer) {
jsonArray.add(objectVal);
} else {
jsonArray.add(objectVal);
}
}
}
jsonObjectLst.put("JSONDATA", jsonArray);
return jsonObjectLst;
}
The important points about Java ArrayList class are:
Java ArrayList class can contain duplicate elements.
Java ArrayList class maintains insertion order.
Java ArrayList class is non synchronized.
Java ArrayList allows random access because array works at the index basis.
In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
Hierarchy of ArrayList class
As shown in above diagram, Java ArrayList class extends AbstractList class which implements List interface. The List interface extends Collection and Iterable interfaces in hierarchical order.
ArrayList class declaration
Let's see the declaration for java.util.ArrayList class.
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Constructors of Java ArrayList
ArrayList()
It is used to build an empty array list.
ArrayList(Collection c)
ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. But, sometimes, you may not know until run time precisely how large an array you need. To handle this situation, the Collections Framework defines ArrayList. In essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded.
No comments:
Post a Comment