Monday, May 18, 2015

Convert ArrayList to JsonArray

Java code to convert a data list into json array
 
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;

class Data{
    int id;
    String name;
   
    public Data(int id, String name){
        this.id = id;
        this.name = name;
    }
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class LISTTEST {
    public static void main(String[] args) {
   
    List dataList = new ArrayList<Data>();
    dataList.add(new Data(1,"One"));
    dataList.add(new Data(2,"Two"));
   
    Gson gson = new GsonBuilder().create();
    JsonArray myCustomArray = gson.toJsonTree(dataList).getAsJsonArray();
   
    System.out.println(myCustomArray);
   
    }
}


OUTPUT

[{"id":1,"name":"One"},{"id":2,"name":"Two"}]

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...