更新时间:2022-03-30 22:15:16
使用GSON库。以下是示例代码
Use GSON library for that. Here is the sample code
List<String> foo = new ArrayList<String>();
foo.add("A");
foo.add("B");
foo.add("C");
String json = new Gson().toJson(foo );
这是Gson的maven依赖
Here is the maven dependency for Gson
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
或者你可以直接从这里下载jar并把它放在你的班级路径中
Or you can directly download jar from here and put it in your class path
http://code.google.com/p/google-gson/downloads/detail?name=gson-1.0.jar&can=4&q=
要将Json发送到客户端,您可以使用spring或简单的servlet添加此代码
To send Json to client you can use spring or in simple servlet add this code
response.getWriter()。write(json );
response.getWriter().write(json);