Json数据样式
为了便于理解我们先来看看Json的数据样式:
1. 单个数据对象
{
"id": 100,
"body": "It is my post",
"number": 0.13,
"created_at": "2014-05-22 19:12:38"
}
2. 多组数据
这是一组Json数据,可以看到就像数组一样,一组数据是用[]围起来的,内部{}围起来的就是单个的数据对象
[{
"id": 100,
"body": "It is my post1",
"number": 0.13,
"created_at": "2014-05-20 19:12:38"
},
{
"id": 101,
"body": "It is my post2",
"number": 0.14,
"created_at": "2014-05-22 19:12:38"
}]
3. 嵌套数据
这段数据是用{}围起来的所以是单个数据对象,我们又能发现其内部又是一个数据对象,名字叫foo2.这就是嵌套的Json数据
{
"id": 100,
"body": "It is my post",
"number": 0.13,
"created_at": "2014-05-22 19:12:38",
"foo2": {
"id": 200,
"name": "haha"
}
}
json在线解析:https://www.bejson.com/ Gson(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为串行化Java对象为JSON字符串,或反串行化JSON字符串成Java对象。 Gson实际就是json使用中的一个jar包使用gson需要在工程中要添加Gson.jar包。 Java对象和Json之间的互转,一般用的比较多的两个类库是Jackson和Gson,firstson。 其最大的优点在于可以减少由于格式的错误导致程序异常,引用这个类可以自动严格按照JSON语法规则(syntax rules)创建JSON text。 |
基础概念:
Serialization : 序列化,将Json字符串转换为Java对象
Deserialization:反序列化,将java字符串转换成Java对象
Gson的两个基础方法
toJson();– 转换java 对象到JSON
public static void main(String[] args) {
Gson gson = new GsonBuilder().create();
gson.toJson("Hello", System.out);
gson.toJson(123, System.out);
}
fromJson(); – 转换JSON到java对象
Gson的创建方式一:直接new Gson对象
Gson g = new Gson();
//json ---->>.object
Child c = g.fromJson(json, Child.class);
System.out.println(c.getChildFoo().getId());
Gson g = new Gson();
Info i = g.fromJson(json, Info.class);
System.out.println(i.getWeatherinfo().getTime());
// object ---->>json
String str = g.toJson(i);
System.out.println(str);
实例训练:
1. 如果Json数据是嵌套定义的,那么这个类中就的成员变量肯定有个类对象,一般这个类包含内部类
{
"id": 100,
"body": "It is my post",
"number": 0.13,
"created_at": "2014-05-22 19:12:38",
"childFoo": {
"id": 200,
"name": "jack"
}
}
第一个ChildFoo类
package com.lanou.entity;
public class ChildFoo {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二个Child类
package com.lanou.entity;
public class Child {
private int id;
private String body;
private double number;
private String created_at;
private ChildFoo childFoo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public ChildFoo getChildFoo() {
return childFoo;
}
public void setChildFoo(ChildFoo childFoo) {
this.childFoo = childFoo;
}
}
Json测试类
package com.lanou.test;
import org.junit.Test;
import com.google.gson.Gson;
import com.lanou.entity.Child;
import com.lanou.entity.Info;
public class JsonTest {
@Test
public void json() {
String json="{ \"id\": 100,\r\n" +
" \"body\": \"It is my post\"," +
" \"number\": 0.13," +
" \"created_at\": \"2014-05-22 19:12:38\"," +
" \"childFoo\": {\r\n" +
" \"id\": 200,\r\n" +
" \"name\": \"jack\"\r\n" +
" }" +
"}";
Gson g = new Gson();
//json ---->>.object
Child c = g.fromJson(json, Child.class);
System.out.println(c.getChildFoo().getId());
}
}
2.天气预报解析:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"22","WD":"北风","WS":"2级","SD":"19%","WSE":"2","time":"16:05","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}
第一个Weatherinfo类
package com.lanou.entity;
public class Weatherinfo {
private String city;
private String cityid;
private String temp;
private String WD;
private String WS;
private String WSE;
private String time;
private String isRadar;
private String Radar;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWD() {
return WD;
}
public void setWD(String wD) {
WD = wD;
}
public String getWS() {
return WS;
}
public void setWS(String wS) {
WS = wS;
}
public String getWSE() {
return WSE;
}
public void setWSE(String wSE) {
WSE = wSE;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getIsRadar() {
return isRadar;
}
public void setIsRadar(String isRadar) {
this.isRadar = isRadar;
}
public String getRadar() {
return Radar;
}
public void setRadar(String radar) {
Radar = radar;
}
}
第二个info类
package com.lanou.entity;
public class Info {
private Weatherinfo weatherinfo;
public Weatherinfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(Weatherinfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
Json测试类
package com.lanou.test;
import org.junit.Test;
import com.google.gson.Gson;
import com.lanou.entity.Child;
import com.lanou.entity.Info;
public class JsonTest {
@Test
public void json2() {
String json = "{\r\n" +
" \"weatherinfo\": {\r\n" +
" \"city\": \"北京\",\r\n" +
" \"cityid\": \"101010100\",\r\n" +
" \"temp\": \"22\",\r\n" +
" \"WD\": \"北风\",\r\n" +
" \"WS\": \"2级\",\r\n" +
" \"SD\": \"19%\",\r\n" +
" \"WSE\": \"2\",\r\n" +
" \"time\": \"16:05\",\r\n" +
" \"isRadar\": \"1\",\r\n" +
" \"Radar\": \"JC_RADAR_AZ9010_JB\"\r\n" +
" }\r\n" +
"}";
Gson g = new Gson();
Info i = g.fromJson(json, Info.class);
System.out.println(i.getWeatherinfo().getTime());
// object ---->>>json
String str = g.toJson(i);
System.out.println(str);
}
}