Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert List to a JSON Object using GSON?

I have a List which I need to convert into JSON Object using GSON. My JSON Object has JSON Array in it.

public class DataResponse {      private List<ClientResponse> apps;      // getters and setters      public static class ClientResponse {         private double mean;         private double deviation;         private int code;         private String pack;         private int version;          // getters and setters     } } 

Below is my code in which I need to convert my List to JSON Object which has JSON Array in it -

public void marshal(Object response) {      List<DataResponse.ClientResponse> clientResponse = ((DataResponse) response).getClientResponse();      // now how do I convert clientResponse list to JSON Object which has JSON Array in it using GSON?      // String jsonObject = ?? } 

As of now, I only have two items in List - So I need my JSON Object like this -

{      "apps":[         {            "mean":1.2,          "deviation":1.3          "code":100,          "pack":"hello",          "version":1       },       {            "mean":1.5,          "deviation":1.1          "code":200,          "pack":"world",          "version":2       }    ] } 

What is the best way to do this?

like image 340
AKIWEB Avatar asked Jan 11 '15 23:01

AKIWEB


People also ask

Can GSON convert list to JSON?

A Gson is a library that can be used to convert Java Objects to JSON representation.

How do I add a list to a JSON object?

JSONObject obj = new JSONObject(); List<String> sList = new ArrayList<String>(); sList. add("val1"); sList. add("val2"); obj. put("list", sList);

How do you convert a list to a JSON object in Python?

To convert a list to json in Python, use the json. dumps() method. The json. dumps() is a built-in function that takes a list as an argument and returns the json value.

How do you serialize with GSON?

Serialization – Write JSON using Gson Serialization in the context of Gson means converting a Java object to its JSON representation. In order to do the serialization, we need to create the Gson object, which handles the conversion. Next, we need to call the function toJson() and pass the User object. Program output.


2 Answers

There is a sample from google gson documentation on how to actually convert the list to json string:

Type listType = new TypeToken<List<String>>() {}.getType();  List<String> target = new LinkedList<String>();  target.add("blah");   Gson gson = new Gson();  String json = gson.toJson(target, listType);  List<String> target2 = gson.fromJson(json, listType); 

You need to set the type of list in toJson method and pass the list object to convert it to json string or vice versa.

like image 113
Rod_Algonquin Avatar answered Oct 15 '22 01:10

Rod_Algonquin


If response in your marshal method is a DataResponse, then that's what you should be serializing.

Gson gson = new Gson(); gson.toJson(response); 

That will give you the JSON output you are looking for.

like image 40
Sotirios Delimanolis Avatar answered Oct 15 '22 01:10

Sotirios Delimanolis