Google之gson的使用



public class JsonTest {

public static void main(String[] args) {
Gson gson = new Gson();
SimpleDateFormat simple = new SimpleDateFormat("yy年MM月dd日 HH:mm");
User u1 = new User();
u1.setAge(200);
u1.setName("jingxuan1");
u1.setDate(simple.format(new Date()));
User u2 = new User();
u2 .setAge(230);
u2 .setName("jingxuan2");
u2 .setDate(simple.format(new Date()));
List<User> userList = new ArrayList<User>();
userList.add(u1);
userList.add(u2);
//java對象轉化成json對象字符串
String json = gson.toJson(userList);
System.out.println("json字符串對象:" + json);
//json字符串對象轉化成java對象
String jsonStr = "[{\"name\":\"jingxuan1\",\"age\":200,\"date\":\"13年09月04日 16:52\"},{\"name\":\"jingxuan2\",\"age\":230,\"date\":\"13年09月04日 16:52\"}]";
//創建List<User>的Type
TypeToken<List<User>> listType = new TypeToken<List<User>>(){};
List<User> users = gson.fromJson(jsonStr, listType.getType());
for(User u : users){
System.out.println("Java對象:姓名:" + u.getName() + ",年齡:" + u.getAge() + ",註冊時間:" + u.getDate());
}
}
}


json:數據傳輸格式,比xml較簡單。 
下面是Ajax使用json的eg:
前臺頁面:
<html>
  <head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
/*function loadXmlDoc(){
var xmlhttp;
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("div1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("post","AjaxServlet?t="+Math.random(),true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("username=jingxuan&&age=20");
}*/
/*$(function(){
$("#button").click(function(){
$.ajax({
url:"AjaxServlet",
type:"get",
data:"username=jingxuan1990&age=20",
success:function(data){
$("#div1").html(data);
}
});
});
});
*/
 
$(function(){
$("#username").blur(function(){
var username = $(this).val();
if(username.length !=0){
if(username.length >= 6 && username.length <= 20){
$.post("AjaxServlet", {username:username}, function(data){
//處理json數據
var temp = "";
for(var i = 0; i < data.length; i++){
temp += "姓名:"+data[i].name + "年齡:" + data[i].age + "<br/>" ;
}
$("#div1").html(temp);
},"json");
}
else{
$("#nameShow").css("display","inline").html("用戶名必須在6-20個字符之間");
}
}
else{
$("#nameShow").css("display","inline").html("用戶名不能爲空!");
}
});
});
</script>
<style type="text/css">
.show
{
font-family: "微軟雅黑";
font-size:"12px";
color:red;
display:none;
margin-left:10px;
}
</style>
  </head>
  
  <body>
  <input type="text" name="username" id="username"/><span id="nameShow" class="show"></span>
  <div id="div1"></div>
  </body>
</html>
 
後臺servlet頁面:
public class AjaxServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Gson gson = new Gson();
SimpleDateFormat simple = new SimpleDateFormat("yy年MM月dd日 HH:mm");
User u1 = new User();
u1.setAge(200);
u1.setName("jingxuan1");
u1.setDate(simple.format(new Date()));
User u2 = new User();
u2 .setAge(230);
u2 .setName("jingxuan2");
u2 .setDate(simple.format(new Date()));
List<User> userList = new ArrayList<User>();
userList.add(u1);
userList.add(u2);
String json = gson.toJson(userList);
out.print(json);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章