Saturday, 16 March 2019

Create JSONArray from a collection, list, set, queue


JSONArray constructor takes a collection as argument and construct JSONArray instance from the collection elements.

List<String> hobbies = new ArrayList<> ();
hobbies.add("Chess");
hobbies.add("Tennis");
hobbies.add("Cricket");

JSONArray jsonArray = new JSONArray(hobbies);

App.java
package com.sample;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

public class App {

 public static void main(String args[]) {
  List<String> hobbies = new ArrayList<> ();
  hobbies.add("Chess");
  hobbies.add("Tennis");
  hobbies.add("Cricket");
  
  JSONArray jsonArray = new JSONArray(hobbies);
 

  System.out.println(jsonArray);

 }
}

Output
["Chess","Tennis","Cricket"]


Previous                                                 Next                                                 Home

No comments:

Post a Comment