Navamani Samuel
Navamani Samuel

Reputation: 568

How to Create JSONArray for a List<Class name>

I have a class called

class Student {
   String name;
   String age;
}

I have a method that returns List object like

public List<Student> getList(){

 List<Student> li =new ArrayList();
 ....

 li.add(new Student('aaa','12'));
 ... 

 return li;    
}

I need to convert that list into JSONArray like this

[{"name":"sam","age":"12"},{"name":"sri","age":"5"}]

Can anyone help me to get this?

Upvotes: 20

Views: 95052

Answers (9)

tribhuwan kumar
tribhuwan kumar

Reputation: 1

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JSONObject json = new JSONObject();

        json.put("userId","");
            
        JSONObject que1 = new JSONObject();
        que1.put("questionId", "");
        que1.put("groupId", "");
        que1.put("answer", "");
        JSONObject que2 = new JSONObject();
        que2.put("questionId", "");
        que2.put("groupId", "");
        que2.put("answer", "");
        JSONObject que3 = new JSONObject();
        que3.put("questionId", "");
        que3.put("groupId", "");
        que3.put("answer", "");
        
        JSONArray jsonArray =new JSONArray();
        jsonArray.add(que1);
        jsonArray.add(que2);
        jsonArray.add(que3);
        
        json.put("answers", jsonArray);

        
        System.out.println("Answer request : "+json.toString());
        
        

    }


}

Upvotes: 0

mahfuj asif
mahfuj asif

Reputation: 1979

You can use ObjectMapper from 'com.fasterxml.jackson.core' like this

List<Student> list = Arrays.asList(new Student("sam", "12"), new Student("sri", "5"));
ObjectMapper objectMapper = new ObjectMapper();
String listAsString = objectMapper.writeValueAsString(list));

Upvotes: 0

vitralyoz
vitralyoz

Reputation: 618

Create JSONArray like below.

JSONArray jsArray = new JSONArray(arrayList);

Upvotes: 5

Mohan Kumar
Mohan Kumar

Reputation: 334

I think you need not download the jettison jar file.

Using JSONArray and JSONObject you can easily convert that list into JSON object like @Juniad answer

Upvotes: 4

JHS
JHS

Reputation: 7871

You will have to include the jettison jar in you project and import the required classes.

JSONObject jObject = new JSONObject();
try
{
    JSONArray jArray = new JSONArray();
    for (Student student : sudentList)
    {
         JSONObject studentJSON = new JSONObject();
         studentJSON.put("name", student.getName());
         studentJSON.put("age", student.getAge());
         jArray.put(studentJSON);
    }
    jObject.put("StudentList", jArray);
} catch (JSONException jse) {
    jse.printStacktrace();
}

Upvotes: 21

Mandar Pandit
Mandar Pandit

Reputation: 2209

Using Gson Library it will be very simple.

From JSON String to ArrayList of Object as:

Type listType = 
     new TypeToken<ArrayList<Student>>(){}.getType();
ArrayList<Student> yourClassList = new Gson().fromJson(jsonArray, listType);

And to Json from Array List of Object as:

ArrayList<Student> sampleList = new ArrayList<Student>();
String json = new Gson().toJson(sampleList);

The Gson Library is more simple to use than JSONObject and JSONArray implementation.

Upvotes: 23

kundan bora
kundan bora

Reputation: 3889

When you want to map Object to json directly or want to convert json to object, you can use GSON library . this will give you more flexibility and control.

Download link - http://code.google.com/p/google-gson/

Tutorial link - http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Upvotes: 1

W. Goeman
W. Goeman

Reputation: 1714

json-lib is likely the library you are looking for. you can find som examples of usage here.

Upvotes: 1

Alex Stybaev
Alex Stybaev

Reputation: 4693

try gson: Serializing-and-Deserializing-Generic-Types

Upvotes: 1

Related Questions