Gson操作

本文主要总结对json的操作

用到struts2和gson2个包。


action为

package action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import model.User;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ListAction extends ActionSupport {

	private static final long serialVersionUID = 7514100921793771035L;
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String execute() {
		return null;
	}

	public String Gson() throws Exception {
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		response.setContentType("text/html;charset=UTF-8");
		User u1 = new User(1,"李文","aaa");
		User u2 = new User(2,"李二","eee");
		User u3 = new User(3,"李三","33a");
		User u4 = new User(4,"王四","bbb");
		List<User> list = new ArrayList<User> ();
		
		String newName = new String(name.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(name+"--"+age+"--"+newName+"--");
		
		list.add(u1);
		list.add(u2);
		list.add(u3);
		list.add(u4);
		
		Gson gson = new Gson();
		JsonElement element = gson.toJsonTree(list);
		String json = gson.toJson(element);
		response.getWriter().print(json);
		System.out.println(json);
		return null;
	}
}

struts.xml为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="UTF-8" />


	<package name="myDemo" extends="struts-default" namespace="/">
	
		<action name="lists" class="action.ListAction" method="Gson">
		</action>
		
		<action name="*">
			<result>/{1}.jsp</result>
		</action>
	</package>
</struts>    


jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html;UTF-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
	<script type="text/javascript">
	var msg = "";
	$(document).ready(function(){
		$.getJSON("lists.action",
				{name:"你好",
				 age:"3"
				 },
				function(data){
					$.each(data,function(i,temp){
						msg += "id=" + temp.userId+";name=" 
									 + temp.userName+";password=" 
									 + temp.passWord+";\n";
						$("p").append(temp.userId).append(temp.userName).append(temp.passWord).append("\n");
					});
					alert(msg);
		});	
	});
	</script>
  </head>
  
  <body>
    aaaaaaaaaa
    <hr/>
  <p></p>
  </body>
</html>
结果如下:




总结:

js传参数有乱码问题:

解决:服务端即action里对参赛进行编码更换,与get方法解决乱码一样


从服务端获取的json数据出现中文乱码:

解决: response.setContentType("text/html;charset=UTF-8");


案例下载    疯狂的点击

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值