convert ArrayList MyCustomClass 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 to JSONArray() in Kotlin
put
adds the list as an element to the JSONArray
. Thats not what you want. You want your JSONArray
to represent the list.
JSONArray
offers a constructor for that:
val jsonArray = JSONArray(listOf(1, 2, 3))
But there is a much easier way. You don't need to worry about single properties. Just pass the whole POJO.
Let's say you have this:
class QuoteData(val id: Int, val quoteId: Int, travellerId: Int?)
class TravelerData(val userQuoteTravellers: List<QuoteData>)
val travelerData = TravelerData(listOf(QuoteData(1354, 546, null)))
You just have to pass travelerData
to the JSONArray
constructor:
val travelerDataJson = JSONArray(travelerData)
and it will be represented like this:
"userQuoteTravellers": [
{
"id": 1354,
"quoteId": 526,
"travellerId": null
} ]
How to convert arrayList String to json object
Try this
dataBase = mHelper.getWritableDatabase();
mCursor = dataBase.rawQuery("SELECT * FROM "
+ DBHelper.TABLE_NAME + " WHERE " + DBHelper.KEY_COUNT
+ " IS NOT NULL AND " + DBHelper.KEY_COUNT + " != '0'", null);
product_price.clear();
productprice = 0;
JSONObject Root = new JSONObject();
JSONArray productArray = new JSONArray();
if (mCursor.moveToFirst()) {
do {
JSONObject product = new JSONObject();
/*
product_price.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PRODUCT_TOTAL_PRICE)));
product_name.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PNAME)));
product_quantity.add(mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_COUNT)));*/
product.put("product_name", mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PRODUCT_TOTAL_PRICE)));
product.put("product_total", mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_PNAME)));
product.put("product_quantity", mCursor.getString(mCursor
.getColumnIndex(DBHelper.KEY_COUNT)));
productArray.put(contact);
} while (mCursor.moveToNext());
Root.put( productArray);
}
How to convert List View items to Json Format
// try this code
JSONObject input = new JSONObject();
JSONArray array = new JSONArray();
for (int i = 0; i < alSets.size(); i++)
{
JSONObject obj_set1 = new JSONObject();
String setNo= "Set " + String.valueOf(i+1);
obj_set1.put("SetId", i + 1);
array.put(obj_set1);
}
input("Key", input);
JsonObjectRequest request = new JsonObjectRequest(requestURL,input, stringListener, errorListener);
request.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
addToRequestQueue(request);
how to convert List object into org.json.JSONArray?
Since arrays are objects in java and being treated as references instead of values so to get the string representation of your array you can use Arrays#toString
obj.put("business_images", Arrays.toString(business_images) );
To get JSONArray use
obj.put("business_images", new JSONArray(Arrays.toString(business_images)));
and since JSONObject support int,long,double,boolean
so you don't need to promote primitive to String but if your REST API expecting all as string then use String.valueOf
for performance efficiency
obj.put("chat_id", String.valueOf(chat_id));
Related Topics
Find Out If Listview Is Scrolled to the Bottom
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
Android Maps API V2 with Custom Markers
Custom Row in a Listpreference
Android: Change Shape Color in Runtime
Linkedlist Put into Intent Extra Gets Recast to Arraylist When Retrieving in Next Activity
Send Data Back to the Script Which Started the Activity via Adb Shell Am Start
Shadow Effect for a Text in Android
What Is the Class R in Android
Programmatically Relaunch/Recreate an Activity
Error Importing Holoeverywhere
Put and Get String Array from Shared Preferences
Neither User 10102 Nor Current Process Has Android.Permission.Read_Phone_State
Google Places API Request Denied for Android Autocomplete, Even with the Right API Key