Day04ajax中数据交换机制(json)串 $.parseJSON,gson.toJSON
2018-01-05 17:04
357 查看
1.ajax中数据交换机制
数据转换图
2.
gson组件
Gson gson = new Gson();
String jsonString = gson.toJson(要转为json的对象);
作用:google提供一个专门用于把java对象或者集合转换成JSON字符串的类库
使用的jar包:(在本人的资源库中,可以进行下载使用)
3.
$.parseJSON()
作用:把json字符串转换成js对象 js数组
实例
1.gson
User:
Address:
TestGson1.java
testparseJson.jsp:
数据转换图
2.
gson组件
Gson gson = new Gson();
String jsonString = gson.toJson(要转为json的对象);
作用:google提供一个专门用于把java对象或者集合转换成JSON字符串的类库
使用的jar包:(在本人的资源库中,可以进行下载使用)
Address address = new Address("bj","100000001"); User u = new User(1,"suns","123456",address); Gson gson = new Gson(); String jsonString = gson.toJson(u); System.out.println(jsonString); 结果:{"id":1,"name":"suns","password":"123456","address":{"city":"bj","zipcode":"100000001"}}
3.
$.parseJSON()
作用:把json字符串转换成js对象 js数组
var jsonString = "{\"id\":1,\"name\":\"suns\",\"password\":\"123456\"}"; var user = $.parseJSON(jsonString); alert(user.id);
实例
1.gson
User:
package com.ajax; public class User { private Integer id; private String name; private String password; private Address address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public User(Integer id, String name, String password,Address address) { super(); this.id = id; this.name = name; this.password = password; this.address = address; } public User() { super(); } }
Address:
package com.ajax; public class Address { private String city; private String zipcode; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } public Address(String city, String zipcode) { super(); this.city = city; this.zipcode = zipcode; } public Address() { super(); } }
TestGson1.java
package com.ajax; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.google.gson.Gson; public class TestGson1 { /*@Test public void test1() { User u = new User(1,"suns","123456");//json字符 Gson gson = new Gson(); String jsonString = gson.toJson(u); System.out.println(jsonString); //运行结果 //{"id":1,"name":"suns","password":"123456"} } @Test public void test2() { String[] names = new String[]{"suns",< 4000 span class="hljs-string">"huxz"}; Gson gson = new Gson(); String jsonString = gson.toJson(names); System.out.println(jsonString); //运行结果 //["suns","huxz"] } @Test public void test3() { // 集合外为综括号,内为大括号 List<User> users = new ArrayList<User>(); User u1 = new User(1,"suns","123456"); User u2 = new User(2,"zhang","123456"); users.add(u1); users.add(u2); Gson gson = new Gson(); String jsonString = gson.toJson(users); System.out.println(jsonString); //运行结果 //[{"id":1,"name":"suns","password":"123456"},{"id":2,"name":"zhang","password":"123456"}] }*/ @Test public void test4() { // 集合外为综括号,内为大括号 Address address = new Address("bj","100000001"); User u = new User(1,"suns","123456",address); Gson gson = new Gson(); String jsonString = gson.toJson(u); System.out.println(jsonString); //运行结果 //{"id":1,"name":"suns","password":"123456","address":{"city":"bj","zipcode":"100000001"}} } }
testparseJson.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
//parseJSON 将json对象转化为js对象
var jsonString = "{\"id\":1,\"name\":\"suns\",\"password\":\"123456\"}"; var user = $.parseJSON(jsonString); alert(user.id);
alert(user.name);
alert(user.password);
var jsonArray = "[\"suns\",\"huxz\"]";
var names = $.parseJSON(jsonArray);
for( var i = 0; i < names.length; i++){
alert(names[i]);
}
</script>
</head>
<body>
</body>
</html>
相关文章推荐
- Android:使用Gson解析复杂的JSON数据
- 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题
- 数据交换格式XML和JSON对比
- XML和JSON两种数据交换格式的比较
- 使用GSON解析json数据
- Android实战——GsonFormat、Gson、fastjson快速搞定JSON数据解析
- Android webview与js 交换JSON对象数据
- 在.NET使用JSON作为数据交换格式
- Gson解析嵌套JSON格式数据
- 使用GSON 解析JSON 数据
- Andorid中使用Gson和Fast-json解析库解析JSON数据---第三方库学习笔记(二)
- Axis2与Android的Json(Gson)通信传数据
- Gson解析复杂的json数据[转]
- 如何用Gson解析复杂的Json数据
- Android中Gson解析json数据使用@SerializedName注解与java对象不匹配的字段
- 【6】页面数据和控件的自动交换机制
- Json轻量级数据交换格式
- 关于Gson解析Okhttp返回的json数据遇到的问题
- GSON解析复杂json数据格式