82516

convert java arraylist to array using for loop

Question:

I have viewed the many similar topics here, none have allowed me to resolve:

//how to Convert this arraylist to array string using loop public static void main(String[] args) { Main m = new Main(); List<JSONObject> jObj = m.getJsonObject(); for (int i = 0; i < jObj.size(); i++) { System.out.println(jObj.get(i)); } }

The many examples I have seen all create an array list by adding in the code, I already have the arrayList. I just need to modify it as an array string and not an arrayList so that I may properly format it as a JSON string with

String responseStr = "{\"data\":" +

Here is the entire bean :

package com.queryData.main; import com.queryData.dao.DataDAO; import com.queryData.services.JsonServices; import java.sql.ResultSet; import java.util.List; import org.json.JSONObject; public class Main { ResultSet resultSet = null; DataDAO datadao = new DataDAO(); public List<JSONObject> getJsonObject() { resultSet = datadao.getResultSet(); List<JSONObject> resList = JsonServices.getFormattedResult(resultSet); return resList; } //how to Convert this arraylist to array string using loop public static void main(String[] args) { Main m = new Main(); List<JSONObject> jObj = m.getJsonObject(); for (int i = 0; i < jObj.size(); i++) { System.out.println(jObj.get(i)); } } }

I tried the following code, but it does not work.

package com.queryData.main; import com.queryData.dao.DataDAO; import com.queryData.services.JsonServices; import java.sql.ResultSet; import java.util.List; import org.json.JSONObject; public class Main { ResultSet resultSet = null; DataDAO datadao = new DataDAO(); public List<JSONObject> getJsonObject() { resultSet = datadao.getResultSet(); List<JSONObject> resList = JsonServices.getFormattedResult(resultSet); return resList; } //how to Convert this arraylist to array string using loop public static void main(String[] args) { Main m = new Main(); List<JSONObject> jObj = m.getJsonObject(); for (int i = 0; i < jObj.size(); i++) { //System.out.println(jObj.get(i)); } String[] stringArr = jObj.toArray( new String[] {} ); for ( String element : stringArr ) { System.out.println( element ); } } }

Any suggestions greatly appreciated. Thanks

Answer1:

Use StringBuilder to build String :

List<Object> jObj = m.getJsonObject(); StringBuilder sb = new StringBuilder(); for(int i =0 ; i < jObj.size(); i++){ sb.append(jObj.get(i).toString()); }

Finally sb.toString() will be your desired String.

Answer2:

you can make some changes : List supports trick like

List<Object> jObj = m.getJsonObject(); for(int i =0 ; i < jObj.size(); i++){ jObj.add(jObj.get(i).toString(), i); // change toString() to whatever u need }

Recommend

  • Java Parser Json as array multidimensional
  • Which is the best way to parse JSON data in Android?
  • Get max ID and min ID from specific column in MySQL php
  • Android Login- checking if username and password match with MySQL database
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • Reading a file into a multidimensional array
  • How do I superscript characters in a UIButton?
  • App restarts from wrong activity
  • Jetty Server not starting: Unable to establish loopback connection
  • Change multiple background-images with jQuery
  • Android screen density dpi vs ppi
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • sending/ receiving email in Java
  • Change an a tag attribute in JavaScript based on screen width
  • Javascript + PHP Encryption with pidCrypt
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • how does django model after text[] in postgresql [duplicate]
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • Add sale price programmatically to product variations
  • Sorting a 2D array using the second column C++
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?