List对象转成json字符串(两种方式)

List对象转成Gson字符串(两种方式)

[b](1)使用Gson gson=new Gson()类中的 gson.toJson(list);方法[/b]

案例 (ajax+json+jquery 省市县级联):
后台代码:

package com.zz.jquery;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.IOP.Encoding;

import com.google.gson.Gson;

public class AjxServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码格式
response.setContentType("application/json;charset=utf-8");
//省
int province = Integer.parseInt(request.getParameter("province"));
//市
int city = Integer.parseInt(request.getParameter("city"));
System.out.println("province==>" + province);
System.out.println("city==>" + city);

//list对象
List list = new ArrayList();
if (province != 0) {
if (province == 1) {
TestJson tj = new TestJson();
//第一个对象
tj.setCityName("郴州市");
tj.setCityNo("10");
//第二个对象
TestJson tj2 = new TestJson();
tj2.setCityName("长沙市");
tj2.setCityNo("11");
//分别将对象加入到list中
list.add(tj);
list.add(tj2);
}

if (province == 2){

TestJson tj = new TestJson();
tj.setCityName("武汉市");
tj.setCityNo("20");

TestJson tj2 = new TestJson();
tj2.setCityName("十堰市");
tj2.setCityNo("21");
list.add(tj);
list.add(tj2);


}
}

if(city!=0){

if(city==10){

TestJson tj = new TestJson();
tj.setCountyNo("101");
tj.setCountyName("苏仙区");

TestJson tj2 = new TestJson();
tj2.setCountyNo("102");
tj2.setCountyName("北湖区");
list.add(tj);
list.add(tj2);
}



}
PrintWriter out = response.getWriter();
Gson gson = new Gson();
String reslut = gson.toJson(list);


System.out.println("reslut:" + reslut);

response.setHeader("pargma", "no-cache");
response.setHeader("cache-control", "no-cache");
out.println(reslut);
out.flush();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);

}

}


前台jsp界面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript">

function sub(){

$.ajax({
type: "post",
url : 'AjxServlet',
data: { province:$('#province').val(), city:$('#city').val()},
success : function(result){
alert(result);
if(eval(result)) {

var html="<option value='11111' >--请选择--</option>";
for(var i=0;i<result.length;i++){


var TestJson =result[i];
var id=TestJson.cityNo;
var name=TestJson.cityName;


html+="<option value="+id+">"+name+"</option>";
};

$("#city option").remove();
$("#city").append(html);
}
}

});

};


function changeCity(){

$.ajax({
type: "post",
url : 'AjxServlet',
data: { city:$('#city').val(),province:$('#province').val()},
success : function(result){

if(eval(result)) {

var html="<option value='0' >--请选择--</option>";

for(var i=2;i<result.length;i++){
var TestJson =result[i];
var id=TestJson.countyNo;
var countName=TestJson.countyName;
html+="<option value="+id+">"+countName+"</option>";
};

$("#county option").remove();
$("#county").append(html);
}
}

});

};
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table width="542" height="168" border="1" cellpadding="0" cellspacing="0">
<tr>
<td height="46" colspan="3"><div align="center">级联ajax+jquery+json测试</div></td>
</tr>
<tr>
<td width="170" height="56">省份:
<label>
<select name="select" id="province" οnchange="sub()">
<option value='0' >--请选择--</option>
<option value="1">湖南省</option>
<option value="2">湖北省</option>
<option value="3">广东省</option>
<option value="4">海南省</option>
</select>
</label></td>
<td width="178">城市:
<label>
<select name="select2" id="city" οnchange="changeCity()">
<option value='0' >----</option>
</select>
</label></td>
<td width="186">县城:
<label>
<select name="select3" id="county">
<option value='0' >----</option>
</select>
</label></td>
</tr>
<tr>
<td height="56"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>

web.xml文件添加以下代码
<servlet>
<servlet-name>AjxServlet</servlet-name>
<servlet-class>com.zz.jquery.AjxServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>AjxServlet</servlet-name>
<url-pattern>/AjxServlet</url-pattern>
</servlet-mapping>


[b](2) 使用JSONArray json=JSONArray.fromobject(list);在调用json.toString()方法转换成字符串[/b]


只需要将上面后台代码的 Gson gson = new Gson();
String reslut = gson.toJson(list);
换成
JSONArray json=JSONArray.fromobject(list);

前提是list必须都为一个完整的对象

所需jar包:
json_java.jar

gson-1.6.jar

具体的项目源码请在附件中下载。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值