Convert normal Java Array or ArrayList to Json Array in android
If you want or need to work with a Java array then you can always use the java.util.Arrays
utility classes' static asList()
method to convert your array to a List
.
Something along those lines should work.
String mStringArray[] = { "String1", "String2" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
Beware that code is written offhand so consider it pseudo-code.
Converting JSONarray to ArrayList
ArrayList<String> listdata = new ArrayList<String>();
JSONArray jArray = (JSONArray)jsonObject;
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.getString(i));
}
}
convert ArrayListMyCustomClass to JSONArray
If I read the JSONArray constructors correctly, you can build them from any Collection (arrayList is a subclass of Collection) like so:
ArrayList<String> list = new ArrayList<String>();
list.add("foo");
list.add("baar");
JSONArray jsArray = new JSONArray(list);
References:
- jsonarray constructor:
http://developer.android.com/reference/org/json/JSONArray.html#JSONArray%28java.util.Collection%29 - collection:
http://developer.android.com/reference/java/util/Collection.html
How to convert ArrayList of custom class to JsonArray in Java?
Below code should work for your case.
List<Customer> customerList = CustomerDB.selectAll();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customer>>() {}.getType());
if (! element.isJsonArray() ) {
// fail appropriately
throw new SomeException();
}
JsonArray jsonArray = element.getAsJsonArray();
Heck, use List
interface to collect values before converting it JSON Tree.
Convert Json Array to normal Java list
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
if (jsonArray != null) {
int len = jsonArray.length();
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
How to convert Arraylist to Json in Java
Not sure if it's exactly what you need, but you can use the GSON library (Link) for ArrayList
to JSON
conversion.
ArrayList<String> list = new ArrayList<String>();
list.add("str1");
list.add("str2");
list.add("str3");
String json = new Gson().toJson(list);
Or in your case:
ArrayList<Domain> list = new ArrayList<Domain>();
list.add(new Domain());
list.add(new Domain());
list.add(new Domain());
String json = new Gson().toJson(list);
If for some reason you find it more convenient, you can also iterate through the ArrayList
and build a JSON
from individual Domain
objects in the list
String toJSON(ArrayList<Domain> list) {
Gson gson = new Gson();
StringBuilder sb = new StringBuilder();
for(Domain d : list) {
sb.append(gson.toJson(d));
}
return sb.toString();
}
Convert JSON array to Java Class Object List
Try this:
try {
JSONObject jsonObject = null;
yourJSONString.replace("\\", "");
jsonObject = new JSONObject(yourJSONString);
String newJSONString = jsonObject.get("GetCardsResult").toString();
JSONArray jsonMainArr = new JSONArray(newJSONString);
//now just loop the json Array
for (int i = 0; i < jsonMainArr.length(); ++i) {
JSONObject rec = jsonMainArr.getJSONObject(i);
card.set_id(rec.get("ID").toString());
//....
}
} catch (JSONException e) {
e.printStackTrace();
}
How to convert a jsonArray into ListString Java?
I assume JSONArray comes from Android. You can just do this:
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
if (jsonArray != null) {
int len = jsonArray.length();
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
Source: Convert Json Array to normal Java list
Related Topics
String-Date Conversion with Nanoseconds
Android: How to Hide Actionbar on Certain Activities
Proguard and Reflection in Android
Why Can't I Use Resources.Getsystem() Without a Runtime Error
Android (Java) Simple Send and Receive with Server - Fast Setup Challenge
Android How to Sort JSONarray of JSONobjects
Rselenium Unknownerror - Java.Lang.Illegalstateexception with Google Chrome
How to Load a Jni .Dylib File with a Dependency Without Getting an Unsatifiedlinkerror
Builder Pattern in Effective Java
Polygon Touch Detection Google Map API V2
Why Do Variable Names Often Start with the Letter 'M'
Networksecurityconfig: No Network Security Config Specified -- Android 7.0 Error
How to Wait for All Tasks in an Threadpoolexecutor to Finish Without Shutting Down the Executor
Spannable on Android for Textview