JSON
简介
(
1
)
JSON
比
XML
数据传输的有效性要高出很多
(
2
)
JSON
完全独立于编程语言。
(
3
)本质就是具有特定格式的字符串
String json1 = "{"id":12,"name":"Tom"}"String json2 = "[{"id":12,"name":"Tom"},{"id":12,"name":"Tom"}]"
( 1 ) Json 数组的结构 : [value1, value2, value3]( 2 )例子:[1, “ab”,[], {“n”:123, “b”:”abc”}] 正确[1, “a”:3] 错误
JSON 解析方向
将
java
对象
(
包含集合
)
转换为
json
格式字符串
在服务器端应用。
![](https://img-blog.csdnimg.cn/59e3066139204f6faf909204e76b4d31.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAd2VpeGluXzM4MTA3NDU3,size_20,color_FFFFFF,t_70,g_se,x_16)
Android 原生技术
将json格式的字符串{}转换为Java对象
1.实体类
package com.example.testapplication.bean;
// 实体类
public class ShopInfo {
private int id;
private String name;
private double price;
private String imagePath;
public ShopInfo(int id, String name, double price, String imagePath) {
this.id = id;
this.name = name;
this.price = price;
this.imagePath = imagePath;
}
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;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
@Override
public String toString() {
return "ShopInfo{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", imagePath='" + imagePath + '\'' +
'}';
}
}
private void jsonToJavaObjectByNative() {
// 1. 创建json数据
String json = "{\n" +
"\t\"id\":2, \"name\":\"大虾\", \n" +
"\t\"price\":12.3, \n" +
"\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" +
"}\n";
ShopInfo shopInfo = null;
// 2. 解析json
try{
JSONObject jsonObject = new JSONObject(json);
// int id = jsonObject.getInt("id");
int id1 = jsonObject.optInt("id");
String name = jsonObject.optString("name");
double price = jsonObject.optDouble("price");
String imagePath = jsonObject.optString("imagePath");
// 封装Java对象
shopInfo = new ShopInfo(id1, name, price, imagePath);
} catch (JSONException e) {
e.printStackTrace();
}
// 显示json数据
tv_native_orignal.setText(json);
tv_native_last.setText(shopInfo.getName());
// tv_native_last.setText(shopInfo.toString());
}
将 json 格式的字符串[]转换为 Java 对象的 List
// 2.将json格式的字符串[] 转换为java对象的List
private void jsonToJavaListByNative() {
// 1 获取或创建JSON数据
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imagePath\": \"http://192.168.10.165:8080/f1.jpg\",\n" +
" \"name\": \"大虾1\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imagePath\": \"http://192.168.10.165:8080/f2.jpg\",\n" +
" \"name\": \"大虾2\",\n" +
" \"price\": 12.5\n" +
" }\n" +
"]";
List<ShopInfo> shops = new ArrayList<>();
// 2.解析json
try{
JSONArray jsonArray = new JSONArray(json);
for(int i =0; i < jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
if(jsonObject != null){
int id = jsonObject.optInt("id");
String name = jsonObject.optString("name");
double price = jsonObject.optDouble("price");
String imagePath = jsonObject.optString("imagePath");
// 封装java对象
ShopInfo shopInfo = new ShopInfo(id,name,price,imagePath);
shops.add(shopInfo);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// 3.显示json数据
tv_native_orignal.setText(json);
tv_native_last.setText(shops.toString());
}
复杂
json
数据解析
package com.example.testapplication.bean;
import java.util.List;
// 实体类
public class DataInfo {
/**
* count : 5
* items : [{"id":45,"title":"坚果"},
* {"id":132,"title":"炒货"},
* {"id":166,"title":"蜜饯"},
* {"id":195,"title":"果脯"},
* {"id":196,"title":"礼盒"}]
*/
private DataBean data;
/**
* data : {"count":5,
* "items":[{"id":45,"title":"坚果"},
* {"id":132,"title":"炒货"},
* {"id":166,"title":"蜜饯"},
* {"id":195,"title":"果脯"},
* {"id":196,"title":"礼盒"}]}
* rs_code : 1000
* rs_msg : success
*/
private String rs_code;
private String rs_msg;
@Override
public String toString() {
return "DataInfo{" +
"data=" + data +
", rs_code='" + rs_code + '\'' +
", rs_msg='" + rs_msg + '\'' +
'}';
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public String getRs_code() {
return rs_code;
}
public void setRs_code(String rs_code) {
this.rs_code = rs_code;
}
public String getRs_msg() {
return rs_msg;
}
public void setRs_msg(String rs_msg) {
this.rs_msg = rs_msg;
}
public static class DataBean{
private int count;
private List<ItemsBean> items;
@Override
public String toString() {
return "DataBean{" +
"count=" + count +
", items=" + items +
'}';
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<ItemsBean> getItems() {
return items;
}
public void setItems(List<ItemsBean> items) {
this.items = items;
}
public static class ItemsBean{
private int id;
private String title;
@Override
public String toString() {
return "ItemsBean{" +
"id=" + id +
", title='" + title + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
private void jsonToJavaofComplex() {
// 1 获取或创建JSON数据
String json = "{\n" +
" \"data\": {\n" +
" \"count\": 5,\n" +
" \"items\": [\n" +
" {\n" +
" \"id\": 45,\n" +
" \"title\": \"坚果\"\n" +
" },\n" +
" {\n" +
" \"id\": 132,\n" +
" \"title\": \"炒货\"\n" +
" },\n" +
" {\n" +
" \"id\": 166,\n" +
" \"title\": \"蜜饯\"\n" +
" },\n" +
" {\n" +
" \"id\": 195,\n" +
" \"title\": \"果脯\"\n" +
" },\n" +
" {\n" +
" \"id\": 196,\n" +
" \"title\": \"礼盒\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"rs_code\": \"1000\",\n" +
" \"rs_msg\": \"success\"\n" +
"}";
// 封装java对象
DataInfo dataInfo = new DataInfo();
// 2.解析json
try{
JSONObject jsonObject = new JSONObject(json);
// 第一层解析
JSONObject data = jsonObject.optJSONObject("data");
String rs_code = jsonObject.optString("rs_code");
String rs_msg = jsonObject.optString("rs_msg");
// 第一层解析
dataInfo.setRs_code(rs_code);
dataInfo.setRs_msg(rs_msg);
DataInfo.DataBean dataBean = new DataInfo.DataBean();
dataInfo.setData(dataBean);
// 第二层解析
int count = data.optInt("count");
JSONArray items = data.optJSONArray("items");
// 第二层数据的封装
dataBean.setCount(count);
List<DataInfo.DataBean.ItemsBean> itemsBeans = new ArrayList<>();
dataBean.setItems(itemsBeans);
// 第三层数据的解析
for(int i = 0;i <items.length();i++){
JSONObject jsonObject1 = items.optJSONObject(i);
if(jsonObject1 != null){
int id = jsonObject1.optInt("id");
String title = jsonObject1.optString("title");
// 第三层数据的封装
DataInfo.DataBean.ItemsBean bean = new DataInfo.DataBean.ItemsBean();
bean.setId(id);
bean.setTitle(title);
itemsBeans.add(bean);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// 3 显示JSON数据
tv_native_orignal.setText(json);
tv_native_last.setText(dataInfo.toString());
}
特殊 json 数据解析
实体类
package com.example.testapplication.bean;
import java.util.List;
public class FilmInfo {
private int code;
private List<FilmBean> list;
@Override
public String toString() {
return "FilmInfo{" +
"code=" + code +
", list=" + list +
'}';
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<FilmBean> getList() {
return list;
}
public void setList(List<FilmBean> list) {
this.list = list;
}
public static class FilmBean{
private String aid;
private String author;
private int coins;
private String copyright;
private String create;
@Override
public String toString() {
return "FilmBean{" +
"aid='" + aid + '\'' +
", author='" + author + '\'' +
", coins=" + coins +
", copyright='" + copyright + '\'' +
", create='" + create + '\'' +
'}';
}
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getCoins() {
return coins;
}
public void setCoins(int coins) {
this.coins = coins;
}
public String getCopyright() {
return copyright;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public String getCreate() {
return create;
}
public void setCreate(String create) {
this.create = create;
}
}
}
private void jsonToJaveOfSpecial() {
// 1 获取或创建JSON数据
String json = "{\n" +
" \"code\": 0,\n" +
" \"list\": {\n" +
" \"0\": {\n" +
" \"aid\": \"6008965\",\n" +
" \"author\": \"哔哩哔哩番剧\",\n" +
" \"coins\": 170,\n" +
" \"copyright\": \"Copy\",\n" +
" \"create\": \"2016-08-25 21:34\"\n" +
" },\n" +
" \"1\": {\n" +
" \"aid\": \"6008938\",\n" +
" \"author\": \"哔哩哔哩番剧\",\n" +
" \"coins\": 404,\n" +
" \"copyright\": \"Copy\",\n" +
" \"create\": \"2016-08-25 21:33\"\n" +
" }\n" +
" }\n" +
"}";
// 封装的java对象
FilmInfo filmInfo = new FilmInfo();
// 解析json
try{
JSONObject jsonObject = new JSONObject(json);
// 第一层解析
int code = jsonObject.optInt("code");
JSONObject list = jsonObject.optJSONObject("list");
// 第一层封装
filmInfo.setCode(code);
List<FilmInfo.FilmBean> list1 = new ArrayList<>();
filmInfo.setList(list1);
// 第二层解析
for(int i = 0;i<list.length();i++){
JSONObject jsonObject1 = list.optJSONObject(i + "");
if(jsonObject1 != null) {
String aid = jsonObject1.optString("aid");
String author = jsonObject1.optString("author");
int coins = jsonObject1.optInt("coins");
String copyright = jsonObject1.optString("copyright");
String create = jsonObject1.optString("create");
// 第二层数据封装
FilmInfo.FilmBean filmBean = new FilmInfo.FilmBean();
filmBean.setAid(aid);
filmBean.setAuthor(author);
filmBean.setCoins(coins);
filmBean.setCopyright(copyright);
filmBean.setCreate(create);
list1.add(filmBean);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// 3 显示JSON数据
tv_native_orignal.setText(json);
tv_native_last.setText(filmInfo.toString());
}