JSON(JavaScript object notation、JavaScript对象表示法)

1.特点

一种轻量级文本数据交换格式,纯文本的

独立于语言和平台,支持多种编程语言

比XML更小、更快、更容易解析(可以使用js中的eval( )方法进行解析)

2.语法

{ 
    "sites": [
        { "name":"菜鸟教程" , "url":"www.runoob" },

        { "name":"google" , "url":"www.google" },

        { "name":"微博" , "url":"www.weibo" }

    ]
}

[ ]  表示数组

{ }  表示对象

 :   key和value之间的分隔符

 ,   用于分隔每一个单独的数据对象

3.对比XML

<?xml version="1.0" encoding="utf-8" ?>

<sites>
    <site>
        <name>菜鸟教程</name>
        <url>www.runoob</url>
    </site>
    <site>
        <name>google</name>
        <url>www.google</url>
    </site>
    <site>
        <name>微博</name>
        <url>www.weibo</url>
    </site>
</sites>

XML更易于阅读,JSON表示字符更少

4.访问数据

javaScript中:

JSON是纯数据格式,只包含属性,没有方法

sites[0] : { "name":"菜鸟教程" , "url":"www.runoob" }

sites[0].name : "菜鸟教程"

Java中:

原生方法-getJSONArray,getJSONObject,getString,getInteger等

5.JSON对象和JSON字符串

var person={"name":"Asuka","sex":"女","age":"14"}
alert(person.name);
alert(typeof person);
//person为json对象,person为object类型


var person='{"name":"Rei","sex":"女","age":"14"}';
alert(person);
alert(typeof person);
/*person为json字符串,之所以叫json字符串,因为字符串的格式符合json的格式
person类型为string*/

6.在js中的应用

在网页(html文件)使用JSON,一般是JSON和JavaScript结合使用,用JSON存数据,用JavaScript操作数据;

JSON.parse();将JSON 字符串转换为 JavaScript 对象

JSON.stringify();将 JavaScript 值转换为 JSON 字符串

7.Gson和FastJson

Gson的基本使用_JamesLau-pro的博客-CSDN博客https://blog.csdn/qq_40163677/article/details/112412623

FastJson的使用方法_倾城诉的博客-CSDN博客https://blog.csdn/qq_44941808/article/details/111942533

8.在JSP中的使用

//showSong.jsp
$(function(){
	//加载类别下拉框
	var param={"op":"getAllSongType"};
	$.get("SongTypeServlet",param,function(songTypeListstr,status,xhr){
		if(status=="success"){
			//将一个 JSON 字符串转换为 JavaScript 对象
			songTypeList=JSON.parse(songTypeListstr);
			console.log("showSong.jsp:下拉框刷新songTypeList:"+songTypeList);
			//清空音乐类别下拉框
			$("#songTypeList").empty();
			var objstr;
			$.each(songTypeList, function(index,songType) {
				objstr+='<option>'+songType.songTypeName+'</option>';
			});
			$("#songTypeList").append(objstr);
		}
	})
})

//singerServlet.java
protected void getAllSinger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	ArrayList<Singer> singerlist=singerService.getAllSinger();
	Gson gson=new Gson();
	String singerliststr=gson.toJson(singerlist);
	response.getWriter().print(singerliststr);
}

9.在springBoot中的使用

//前端传入的JSON参数
{

  "Team": {
    "teamID": "c24fa65577d34542bb72013ca633006c"
  },

  "studentList": [
    {
      "studentID": "407cff4557c5e538d9707736a4454e00",
      "studentName": "string"
    },
	{
      "studentID": "5e4076b2db5e52fa6e7692d02f1ad6b4",
      "studentName": "string"
    },
	{
      "studentID": "fac6e64003a99e25a87c45120870e028",
      "studentName": "string"
    }
  ]

}


//后端controller

//添加、编辑、保存小组
@ApiOperation("添加、编辑、保存小组")
@RequestMapping(value="/saveTeam",method=RequestMethod.POST)
public WrappedResult InsertTeam(@RequestBody TeamVo teamVo,HttpServletResponse response) {
	Team team=teamVo.getTeam();
	ArrayList<Student> studentList=teamVo.getStudentList();
	Map<String,Object> map=new HashMap<String, Object>();
	String teamID=team.getTeamID();
	String teamName=team.getTeamName();
	String remark=team.getRemark();
	map.put("teamID", teamID);
	map.put("teamName",teamName);
	map.put("remark",remark);

    ......

}
//1.@RequestBody TeamVo teamVo
//@RequestBody把前端传来的json封装为Java实体类TeamVo
//使用实体类中的get,set等方法操作参数teamVo


//jsonTest
@ApiOperation("jsonTest")
@RequestMapping(value="/json",method=RequestMethod.POST)
public WrappedResult jsonTest(@RequestBody JSONObject json,HttpServletResponse response){
	JSONObject kbsTeam=json.getJSONObject("kbsTeam");
	String teamID=kbsTeam.getString("teamID");
	System.out.println("********"+kbsTeam.toString());
	System.out.println("********"+teamID);
	JSONArray jsonArray=json.getJSONArray("studentList");
	for(int i=0;i<jsonArray.size();i++){
		JSONObject j=jsonArray.getJSONObject(i);
		System.out.println("********"+j.toString());
		System.out.print(j.getString("studentID"));
		System.out.println(","+j.getString("studentName"));
	}
	
	......
}
//2.@RequestBody JSONObject json
//@RequestBody把前端传来的json封装为JSONObject实体类
//使用getJSONArray、getJSONObject、get、getString等方法操作参数json

更多推荐

JSON在Java中的使用