1. JSON 的 写法 和 简介

1. JSON 的 写法 和 简介

JSON 很常用啊 也很有用,反正比XML好又快

json简介

json的全称是JavaScript Object Notation,是一种数据交换格式,独立于编程语言,可以用来存储数据,优点是非常利于人类阅读和编写,同时也利于机器的解析和生成,同时还可以提升网络传输效率。json的数据格式是以名-值对的方式存储的,可以将其看做是一个字符串,下面就是一个json格式的数据,对象会放到{}中,数组放到[]中:

两种写法:(JAVASCRIPT 中 写法)

在json中规定的字符编码时utf-8,几乎所有的编程语言都有解析json数据的第三方库,

因此json的应用非常多。在servlet中可以将要返回给前台的数据封装为json格式,

在前台中可以使用javascript来解析,javascript已经内置了json的解析。

json格式的数据是跟语言无关的,在java中也有非常多的第三方库可以解析json格式的数据,常用的有:

fastjson:国人开发的,是目前java语言中最快的json库

gson:google开发

jackson:Tatu Saloranta开发

下面以fastjson为例看下,自己去百度下载啊

fastjson中常用的方法:

JSON.toJSONString

将JavaBean转成json格式的字符串

JSON.parseObject

将json格式的字符串转换为JavaBean

代码演示:

我们创建一个学生类: GET 和 SET 都要有 再来个构造啊【规范点请加空构造 养成好习惯 这里忘记了 随便啦】

package temp;

import java.util.Date;

public class student {

private String name;

private int age;

private char sex;

private Date birthday;

public student1(String name, int age, char sex, Date birthday) {

super();

this.name = name;

this.age = age;

this.sex = sex;

this.birthday = birthday;

}

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 char getSex() {

return sex;

}

public void setSex(char sex) {

this.sex = sex;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

再来个学校类:

package temp;

import java.util.List;

public class school {

private List zspt;

public school(List zspt) {

super();

this.zspt = zspt;

}

public school() {

}

public List getZspt() {

return zspt;

}

public void setZspt(List zspt) {

this.zspt = zspt;

}

}

其次我们尝试用插件去生成JSON 和 解析JSON:

<%@page import="java.text.DateFormat"%>

<%@page import="com.alibaba.fastjson.JSON"%>

<%@page import="temp.school"%>

<%@page import="temp.student"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

Insert title here

<%

student bihu = new student("bihu",18,'男',DateFormat.getDateInstance().parse("2020-01-01"));

student zhangsan = new student("zhangsan",21,'女',DateFormat.getDateInstance().parse("1999-12-03"));

school zspt = new school();

zspt.getZspt().add(bihu);

zspt.getZspt().add(zhangsan);

//开始生成 JSON :

String strJSON = JSON.toJSONString(zspt);

//JSON 转对象:

school objJSON = (school)JSON.parseObject(strJSON, school.class);

//然后分别装入 reques域中 在网页打印:

request.setAttribute("json_str",strJSON );

request.setAttribute("json_obj", objJSON);

//还可以打印: 都没问题的!

System.out.print(objJSON.getZspt().get(0).getName());

System.out.print(objJSON.getZspt().get(0).getBirthday());

%>

json_str : ${ json_str }


json_obj : ${ json_obj }

单层JSON 和 多层JSON 对比:

{"age":18,"birthday":1577808000000,"name":"bihu","sex":"男"}

----------------------------------------------------------------

{"zspt":

[

{"age":18,"birthday":1577808000000,"name":"bihu","sex":"男"},

{"age":21,"birthday":944150400000,"name":"zhangsan","sex":"女"}

]

}

其实不喜欢这种方式解析 JAVA有自带的

相关文章

蚊子一咬一个包,究竟怎么消? 365365bet娱乐场

蚊子一咬一个包,究竟怎么消?

📅 08-11 👁️ 1351
南方公园系列套餐 365bet备用地址

南方公园系列套餐

📅 07-20 👁️ 6356
联想M7205怎么样 365bet备用地址

联想M7205怎么样

📅 08-25 👁️ 2112