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 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 a jsonArray into List String 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
How to convert the simple.JSONArray to normal JSONArray in Java
org.json.simple
and org.json
are different and incompatible libraries.
The use of org.json.simple
shouldn't be recommended as it returns all of its values as an Object
, whereas org.json
allows to dictate what type
to return.
The solution is not to use the org.json.simple.parser.JSONParser
(delete all of the org.json.simple
imports) but to read the String from the relevant file and pass that to the org.json.JSONArray
directly, like so:
String json = FileUtils.readFileToString(new File( "src/main/resources/SampleRequest.json"), "UTF-8" );
JSONArray array = new JSONArray(json);
As you can see, I like to use the FileUtils
from the following package for simplicity's sake, so add this dependency:
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
How to convert json to ArrayList object in Java?
You need to use:
Product p = new Product();
p.setId( jsonArray.getJSONObject(i).getInt("id") );
p.setName( jsonArray.getJSONObject(i).getString("name") );
list.add(p)
All inside the loop, because you need to create a new product each time.
Convert JSONArray to String Array
Take a look at this tutorial.
Also you can parse above json like :
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"));
}
Convert JSONArray to String[] Array
i'm put the JSONArray
to the string toString()
, and i'm convert by regular expression like this ..
public String getImg(String d){
return rubahFormat(d).split(",");
}
public String rubahFormat(String d){
return d.replaceAll("[\\[\\]\\\"]","");
}
Thx ..
JsonArray to Arraylist Conversion
Internally this JsonArray implements list interface so type casting to List is perfectly fine. Moreover iterating each and every element will access list one by one which will be overhead(Performance impact).
If you need alternate way you can use toCollection static method from the same JsonArray class
Generic Method to Convert JSON Array String to List in java
Don’t reinvent the wheel:
public static <T> List<T> stringToClassList(String data, Class<T> convertType, ObjectMapper mapper) {
return mapper.readValue(data, mapper.getTypeFactory().constructCollectionType(List.class, convertType));
}
Also note fixing the return type from List<?>
(less useful) to List<T>
(useful).
Your test method can also be simplified and improved:
@Test
public void testStringToClassList() {
String input = "[\"foo\", \"bar\"]";
List<String> expected = Arrays.asList("foo", "bar");
Assert.assertEquals(expected, stringToClassList(input, String.class));
}
Related Topics
Classcastexception When Casting to the Same Class
How to Pass an Array as Arguments to a Method With Variable Arguments in Java
Should I Instantiate Instance Variables on Declaration or in the Constructor
Android Recyclerview Addition & Removal of Items
How to Get Unique Random Product in Node Firebase
How to Kill a Linux Process in Java with Sigkill Process.Destroy() Does Sigterm
Java Getlocalhost() Unknownhostexception /Etc/Hosts File Differs Linux API
What Is Contained in Code/Internal Sections of Jcmd
In Linux, How to Execute Java Jar File with External Jar Files
Variable Column Names Using Prepared Statements
How to Use Comparator to Define a Custom Sort Order
How to Convert an Arraylist Containing Integers to Primitive Int Array
How to Unzip Files Programmatically in Android
Bitmap Size Exceeds Vm Budget Error Android