How to convert generic list to json in Java.I have class like this..
public class Output
{
public int Keyname { get; set; }
public Object outputvalue{ get; set; } //outvalue may be even a object collection
}
List outputList = new List();
I want to convert outputList into json in Java.After converting i will send it to client.
解决方案
Use GSON library for that. Here is the sample code
List foo = new ArrayList();
foo.add("A");
foo.add("B");
foo.add("C");
String json = new Gson().toJson(foo );
Here is the maven dependency for Gson
com.google.code.gson
gson
2.2.2
compile
Or you can directly download jar from here and put it in your class path
To send Json to client you can use spring or in simple servlet add this code
response.getWriter().write(json);