How to convert Json ArrayList into String Array in Java?


How to convert Json ArrayList into String Array?

Here is my Sample code where I am getting ArrayList data in mAddList variable

private void getCart_Data() {
try {
long sum;
dataBasehelper3 = new DataBasehelper3(this);
mAddList = new ArrayList<Rpt_OrderPlaceCartModel>();
mAddList = dataBasehelper3.getAllPlaceOrder();

//getItems();
ja = new JSONArray();

for (int i = 0; i < mAddList.size(); i++) {
JSONObject jo = new JSONObject();
jo.put("Productname", mAddList.get(i).getProductName());

ArrayList<String> mStringList= new ArrayList<String>();


String [] menu_name;

mStringList.add(mAddList.get(i).getProductName());
mStringList.add(mAddList.get(i).getProductprice());
String[] mStringArray = new String[mStringList.size()];

String name = mAddList.get(i).getProductName();

Log.d("resp", mStringArray.toString());

/* for(int i4 = 0; i < mStringArray.length ; i4++){
Log.d("string is",(String)mStringArray[i4]);
}*/

/*jo.put("productprice", mAddList.get(i).getProductQuantity());
jo.put("productprice", mAddList.get(i).getProductTotalPrice());
jo.put("productprice", mAddList.get(i).getProId());
jo.put("productprice", mAddList.get(i).getRestTableNo());*/

Log.d("getCart_Data11: ","a"+ jo);
ja.put(jo);
Log.d("getCart_Data: ","d"+ ja);
}
System.out.println(ja);
Log.d("getCart_Data: ","d"+ ja);

dataBasehelper3.close();

SalesCartActivity.ListViewAdapter adapter = new SalesCartActivity.ListViewAdapter(this, mAddList);
lv.setAdapter(adapter);
dataBasehelper3.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Thanks


Asked by:- akramyk9
0
: 2917 At:- 11/1/2018 12:54:23 PM
Android ArrayList StringArray Java







1 Answers
profileImage Answered by:- vikas_jk

You would have to loop through all the values and store it in a ArrayList.

Here is the example

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
    list.add(arr.getJSONObject(i).getString("name"));
}

Or you can simply do like this

String[] arr = jsonArray.toString().replace("},{", " ,").split(" ");

Also take a look on few links like

https://javarevisited.blogspot.com/2013/04/convert-json-array-to-string-array-list-java-from.html

https://www.programiz.com/kotlin-programming/examples/convert-list-array

https://viralpatel.net/blogs/convert-arraylist-to-arrays-in-java/

This may help.

1
At:- 11/1/2018 2:50:32 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use