1.JSON定义
是一种轻量级的数据交换格式
2.JSON作用
JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 Web 客户机传递给服务器端程序。这个字符串看起来有点儿古怪,但是 JavaScript 很容易解释它,而且 JSON 可以表示比"名称 / 值对"更复杂的结构。例如,可以表示数组和复杂的对象,而不仅仅是键和值的简单列表
3.为什么使用JSON
JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
4.JSON语法
(0)JSON存储语法
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true 或 false)
数组(在中括号中)
对象(在大括号中)
null
(1)JSON数字(常用)
JSON 数字可以是整型或者浮点型
{ "age":30 }
(2)JSON对象(常用)
{ "name":"kgc" , "pwd":"23434" }
(3)JSON数组(常用)
[
{ "name":"菜鸟教程" , "url":"www.runoob.com" },
{ "name":"google" , "url":"www.google.com" },
{ "name":"微博" , "url":"www.weibo.com" }
]
4.JSON实战
4.1创建工程,数据库,导入jar包,js类库
4.2创建实体类
public class Fruit {
private Integer fruitId;
private String type;
private String breed;
private String area;
private String brief;
private Integer weight;
private BigDecimal price;
public Fruit() {
}
public Fruit(Integer fruitId, String type, String breed, String area, String brief, Integer weight, BigDecimal price) {
this.fruitId = fruitId;
this.type = type;
this.breed = breed;
this.area = area;
this.brief = brief;
this.weight = weight;
this.price = price;
}
public Integer getFruitId() {
return fruitId;
}
public void setFruitId(Integer fruitId) {
this.fruitId = fruitId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getBrief() {
return brief;
}
public void setBrief(String brief) {
this.brief = brief;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
4.3BaseDao工具类
package cn.kgc.dao;
import java.sql.*;
/**
* 连接数据库的工具类
*/
public class BaseDao {
// 1.定义连接对象
protected static Connection conn;
// 2.定义预编译对象
protected static PreparedStatement ps;
// 3.定义结果集对象
protected static ResultSet rs;
//4.定义连接数据库的方法getConn()
public static void getConn(){
try {
//5.注册驱动
Class.fo