JSON
简单JSON
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSON</title>
</head>
<body>
<script type="text/javascript">
/*
JSON是:Java Script Object Notation(数据交换格式)
作用:一种标准的数据交换格式
JSON是一种标准的轻量级数据交换格式。特点:体积小,易解析。
实际开发中,JSON、XML两种数据交换格式使用最多
XML(可扩展标记语言)体积较大,解析麻烦,但语法严谨。使用在例如银行
HTML和XML有父亲SGML(标准通用的标记语言)
HTML主做页面展示则语法松散
XML主做数据储存和描述则语法严谨
JSON语法格式:
var jsonObj={
"属性名":属性值,
"属性名":属性值,
"属性名":属性值,
"属性名":属性值,
...
}
*/
//创建JSON对象(JSON也可称为无类型对象,轻量级)
var studentObj={
"sno":"110",
"sname":"张三",
"sex":"男"
}
//访问JSON对象属性
//第一种方式
alert(studentObj.sno+","+studentObj.sname+","+studentObj.sex);
//没有JSON对象时,定义类,创建对象,访问对象属性
Student=function(sno,sname,sex){
this.sno=sno;
this.sname=sname;
this.sex=sex;
}
var stu=new Student("111","李四","男");
alert(stu.sno+","+stu.sname+","+stu.sex);
//JSON数组
var students[
{"sno":"001","sname":"张三","sex":"男"};
{"sno":"002","sname":"李四","sex":"男"},
{"sno":"003","sname":"王五","sex":"男"}
]
</script>
</body>
</html>
进阶JSON
JSON嵌套
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>复杂一点的JSON</title>
</head>
<body>
<script type="text/javascript">
var user={
"userID":100,
"userName":"Kramer",
"sex":true,
"address":{
"province":"Beijing",
"city":"Beijing",
"street":"daxing",
"zipcode":"112211"
},
"aihao":["smoke","drink"],
}
//访问人名
alert(user.userName+"live in"+user.address.city);
</script>
</body>
</html>
eval()函数
作用是将字符串当作一段JS代码解释并执行。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>eval()</title>
</head>
<body>
<script type="text/javascript">
window.eval("var i=100;");
alert(i);//弹出 100
</script>
</body>
</html>
Java连接数据库,查询数据后,将数据在Java程序中拼接成JSON格式的“字符串”,将JSON格式的字符串响应到服务器。
也就是说Java响应到浏览器的只是一个JSON格式的字符串。
可以使用JSON格式的字符串转换成JSON对象。
Java发过来字符串var fromJava="{\"name\":\"Kramer\",\"password\":\"123\"}"
将以上JSON格式的字符串转换成JSON对象window.eval("var jsonObj"+fromJava)
加下来可以访问JSON对象了。
JSON数据类型
数字型(Number) JavaScript 中的双精度浮点型格式
字符串型(String) 双引号包裹的 Unicode 字符和反斜杠转义字符
布尔型(Boolean) true 或 false
数组(Array) 有序的值序列
值(Value) 可以是字符串,数字,true 或 false,null 等等
对象(Object) 无序的键:值对集合
空格(Whitespace) 可用于任意符号对之间
null 空
使用JSON操作table元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>设置table的tbody</title>
</head>
<body>
<script type="text/javascript">
//有这些json数据
var data={
"total":4,
"emps":[
{"empno":001,"ename":"Kramer","sal":800.00},
{"empno":002,"ename":"Kramer","sal":800.00},
{"empno":003,"ename":"Kramer","sal":800.00},
{"empno":004,"ename":"Kramer","sal":800.00}
]
};
//希望把数据展示到table中
window.onload=function(){
var showInfoBtn=document.getElementById("showInfoBtnId");
showInfoBtn.onclick=function(){
var emps=data.emps;
var html="";
for(var i=0;i<emps.length;i++){
var emp=emps[i];
html+="<tr>"
html+="<th>"+emp.empno+"</th>"
html+="<th>"+emp.ename+"</th>"
html+="<th>"+emp.sal+"</th>"
html+="</tr>"
alert(html);
}
document.getElementById("emptbody").innerHTML=html;
document.getElementById("count").innerHTML=data.total;
}
}
</script>
<input type="button" value="显示员工信息" id="showInfoBtnId">
<h2>员工信息表</h2>
<hr>
<table border="1px" width="40%">
<tr>
<th>员工编号</th>
<th>员工姓名</th>
<th>员工薪水</th>
</tr>
<tbody id="emptbody">
</tbody>
</table>
总记录条数:<span id="count">4</span>
</body>
</html>
解析JSON
public class JsonUtils {
/**
* 根据json数据解析返回一个List<HashMap<String, Object>>集合
* @param json json数据
* @return
*/
public static List<HashMap<String, Object>> getJsonList(String json) {
List<HashMap<String, Object>> dataList;
dataList = new ArrayList<>();
try {
JSONObject rootObject = new JSONObject(json);
JSONObject paramzObject = rootObject.getJSONObject("paramz");
JSONArray feedsArray = paramzObject.getJSONArray("feeds");
for (int i = 0; i < feedsArray.length(); i++) {
JSONObject sonObject = feedsArray.getJSONObject(i);
JSONObject dataObject = sonObject.getJSONObject("data");
String subjectStr = dataObject.getString("subject");
String summaryStr = dataObject.getString("summary");
String coverStr = dataObject.getString("cover");
HashMap<String, Object> map = new HashMap<>();
map.put("subject", subjectStr);
map.put("summary", summaryStr);
map.put("cover", coverStr);
dataList.add(map);
}
return dataList;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
Jackson
常用注解
@JsonProperty注解指定一个属性用于JSON映射,默认情况下映射的JSON属性与注解的属性名称相同,不过可以使用该注解的value值修改JSON属性名,该注解还有一个index属性指定生成JSON属性的顺序,如果有必要的话。
@JsonIgnore注解用于排除某个属性,这样该属性就不会被Jackson序列化和反序列化。
@JsonIgnoreProperties注解是类注解。在序列化为JSON的时候,
@JsonIgnoreProperties({"prop1", "prop2"})会忽略pro1和pro2两个属性。在从JSON反序列化为Java类的时候,@JsonIgnoreProperties(ignoreUnknown=true)会忽略所有没有Getter和Setter的属性。该注解在Java类和JSON不完全匹配的时候很有用。
默认情况下,Jackson会处理所有public的属性和拥有getter方法的属性(反序列化需要setter)
2449

被折叠的 条评论
为什么被折叠?



