当前位置:网站首页>JSON data parsing
JSON data parsing
2022-07-27 16:42:00 【Young people in Baima town】
1_JSON brief introduction
1.1_ brief introduction
JSON The full name is JavaScript Object Notation, It's a lightweight data exchange lattice
type .
1.2_ characteristic
(1)JSON Than XML The efficiency of data transmission is much higher
(2)JSON Completely independent of programming language .
(3) The essence is a string with a specific format
2_JSON data format
2.1_ The overall structure
String json1 = "{"id":12,"name":"Tom"}"
String json2 = "[{"id":12,"name":"Tom"},{"id":12,"name":"Tom"}]"
2.2_Json Array : [ ]
(1)Json The structure of the array : [value1, value2, value3]
(2) Example :
[1, “ab”,[], {“n”:123, “b”:”abc”}] correct
[1, “a”:3] error
2.2_Json object : {}
(1)Json Structure of objects : {key1:value1, key2:value2, key3:value3}
(2)key Data type of : character string
(3)value Data type of : The number 、 character string 、null、json Array []、json object {}
(4) Example :
{“name”:”TOM”, “age”:12} correct
{“aa”:“a”, 3} error
3_JSON Analytical direction
3.1_ take java object ( Contains sets ) Convert to json Format string
Apply on the server side . 3.2_ take json Format string to java object ( Contains sets )
In the client application . 3.3_Json and Java Transformation relationship between
(1)JSON The object in corresponds to Java Objects in the
(2)Json In the array , Corresponding Java The set in
4_JSON Parsing Technology
4.1_Android Native technology
1) characteristic : Programming is relatively cumbersome
4.1.1_ take json Format string {} Convert to Java object
1)API:JsonObject
JSONObject(String json) : take json The string resolves to json object
Xxx getXxx(String name) : according to name, stay json Object to get the corresponding Value
2) Test data
{
"id":2, "name":" Prawns ",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg" }3) Example
// take json Format string {} Convert to Java object
private void jsonToJavaObjectByNative() {
// Get or create JSON data
String json = "{\n" +
"\t\"id\":2, \"name\":\" Prawns \", \n" +"\t\"price\":12.3, \n" +"\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\ "\n" +"}\n";
ShopInfo shopInfo = null;
// analysis 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");
// encapsulation Java object
shopInfo = new ShopInfo(id1, name, price, imagePath);
} catch (JSONException e) {
e.printStackTrace();
}
// Show JSON data
tv_native_orignal.setText(json);
tv_native_last.setText(shopInfo.toString());
}
4.1.2_ take json Format string [] Convert to Java Object's List
1)API:JSONArray
JSONArray(String json) : take json The string resolves to json Array
int length() : obtain json The number of elements in the array
Xxx getXxx(int index) : According to the subscript json The element data corresponding to the array
2) Test data
[ {
"id":1, "name":" Prawns 1",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/f1.jpg"
},
{
"id":2, "name":" Prawns 2",
"price":12.5,
"imagePath":"http://192.168.10.165:8080/f2.jpg" } ]3) Example
// take json Format string [] Convert to Java Object's List
private void jsonToJavaListByNative() {
// Get or create JSON data
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imagePath\":
\"http://192.168.10.165:8080/f1.jpg\",\n" +
" \"name\": \" Prawns 1\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imagePath\":
\"http://192.168.10.165:8080/f2.jpg\",\n" +
" \"name\": \" Prawns 2\",\n" +
" \"price\": 12.5\n" +
" }\n" +
"]";
List<ShopInfo> shops = new ArrayList<>();
// analysis 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");
// encapsulation Java object
ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath);
shops.add(shopInfo);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// Show JSON data
tv_native_orignal.setText(json);
tv_native_last.setText(shops.toString());
}
4.1.3_ complex json Data analysis
1) Test data
{"data": {"count": 5,"items": [{ "id": 45, "title": " nuts " }, {"id": 132, "title": " fry " }, { "id": 166, "title": " Candied fruit " }, { "id": 195, "title": " Preserved fruit " }, { "id": 196, "title": " Gift box " } ] }, "rs_code": "1000","rs_msg": "success"}
2) Example
// complex json Data analysis
private void jsonToJavaOfComplex() {
// Get or create JSON data
String json = "{\n" +
" \"data\": {\n" +
" \"count\": 5,\n" +
" \"items\": [\n" +
" {\n" +
" \"id\": 45,\n" +
" \"title\": \" nuts \"\n" +
" },\n" +
" {\n" +
" \"id\": 132,\n" +
" \"title\": \" fry \"\n" +
" },\n" +
" {\n" +
" \"id\": 166,\n" +
" \"title\": \" Candied fruit \"\n" +
" },\n" +
" {\n" +
" \"id\": 195,\n" +
" \"title\": \" Preserved fruit \"\n" +
" },\n" +
" {\n" +
" \"id\": 196,\n" +
" \"title\": \" Gift box \"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"rs_code\": \"1000\",\n" +
" \"rs_msg\": \"success\"\n" +
"}";
// encapsulation Java object
DataInfo dataInfo = new DataInfo();
// analysis json
try {
JSONObject jsonObject = new JSONObject(json);
// First level analysis
JSONObject data = jsonObject.optJSONObject("data");
String rs_code = jsonObject.optString("rs_code");
String rs_msg = jsonObject.optString("rs_msg");
// The first layer of packaging
dataInfo.setRs_code(rs_code);
dataInfo.setRs_msg(rs_msg);
DataInfo.DataBean dataBean = new DataInfo.DataBean();
dataInfo.setData(dataBean);
// The second level of analysis
int count = data.optInt("count");
JSONArray items = data.optJSONArray("items");
// The second layer of data encapsulation
dataBean.setCount(count);
List<DataInfo.DataBean.ItemsBean> itemsBean = new ArrayList<>();
dataBean.setItems(itemsBean);
// The third level of analysis
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");
// Encapsulation of the third layer data
DataInfo.DataBean.ItemsBean bean = new DataInfo.DataBean.ItemsBean();
bean.setId(id);
bean.setTitle(title);
itemsBean.add(bean);}}
} catch (JSONException e) {
e.printStackTrace();
}
// Show JSON data
tv_native_orignal.setText(json);
tv_native_last.setText(dataInfo.toString());
}
4.1.4_ special json Data analysis
1) Test data
{"code": 0,"list": {
"0": {
"aid": "6008965",
"author": " Bili Bili opera ",
"coins": 170,
"copyright": "Copy",
"create": "2016-08-25 21:34"
},
"1": {
"aid": "6008938",
"author": " Bili Bili opera ",
"coins": 404,
"copyright": "Copy",
"create": "2016-08-25 21:33"
}
}
}2) Example
// (4) special json Data analysis
private void jsonToJavaOfSpecial() {
// 1 Get or create JSON data
String json = "{\n" +
" \"code\": 0,\n" +
" \"list\": {\n" +
" \"0\": {\n" +
" \"aid\": \"6008965\",\n" +
" \"author\": \" Bili Bili opera \",\n" +
" \"coins\": 170,\n" +
" \"copyright\": \"Copy\",\n" +
" \"create\": \"2016-08-25 21:34\"\n" +
" },\n" +
" \"1\": {\n" +
" \"aid\": \"6008938\",\n" +
" \"author\": \" Bili Bili opera \",\n" +
" \"coins\": 404,\n" +
" \"copyright\": \"Copy\",\n" +
" \"create\": \"2016-08-25 21:33\"\n" +
" }\n" +
" }\n" +
"}";
// Create encapsulated Java object
FilmInfo filmInfo = new FilmInfo();
// 2 analysis json
try {
JSONObject jsonObject = new JSONObject(json);
// First level analysis
int code = jsonObject.optInt("code");
JSONObject list = jsonObject.optJSONObject("list");
// The first layer of packaging
filmInfo.setCode(code);
List<FilmInfo.FilmBean> lists = new ArrayList<>();
filmInfo.setList(lists);
// The second level of analysis
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");
// The second layer is data encapsulation
FilmInfo.FilmBean filmBean = new FilmInfo.FilmBean();
filmBean.setAid(aid);
filmBean.setAuthor(author);
filmBean.setCoins(coins);
filmBean.setCopyright(copyright);
filmBean.setCreate(create);
lists.add(filmBean);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// 3 Show JSON data
tv_native_orignal.setText(json);
tv_native_last.setText(filmInfo.toString());
}
4.2_GSON Frame technology
1) characteristic : The coding is simple , Google officially recommends
2) Download address :https://mvnrepository.com/artifact/com.google.code.gson/gson
4.2.1_ take json Format string {} Convert to Java object
1) Use of API
<T> T fromJson(String json, Class<T> classOfT);// take json Object to Java object
Methods
Be careful : requirement json Object key The name of java The property name in the class corresponding to the object should be the same
2) Use steps
(1) take Gson Of jar Package import into project
(2) establish Gson object : Gson gson = new Gson();
(3) By creating Gson Object call fromJson() Method , Return to the JSON Data corresponds to Java object
ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
3) Test data
{
"id":2, "name":" Prawns ",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg" }4) Example
// (1) take json Format string {} Convert to Java object
private void jsonToJavaObject() {
// 1 Get or create json
String json = "{\n" +
"\t\"id\":2, \"name\":\" Prawns \", \n" +
"\t\"price\":12.3, \n" +
"\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\ "\n" +
"}\n";
// 2 analysis json
Gson gson = new Gson();
ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
// 3 Show JSON data
tv_native_orignal.setText(json);
tv_native_last.setText(shopInfo.toString());
}
4.2.2_ take json Format string [] Convert to Java Object's List
1) Use of API
T fromJson(String json, Type typeOfT);// take json Array to Java Object's list
Be careful : requirement json Object key The name of java The property name in the class corresponding to the object should be the same
2) Use steps
(1) take Gson Of jar Package import into project
(2) establish Gson object : Gson gson = new Gson();
(3) By creating Gson Object call fromJson() Method , Return to the JSON Data corresponds to Java aggregate :
List<ShopInfo> shops = gson.fromJson(json, new
TypeToken<List<ShopInfo>>() {}.getType());
3) Test data
[
{
"id": 1,
"imagePath": "http://192.168.10.165:8080/f1.jpg",
"name": " Prawns 1",
"price": 12.3
},{
"id": 2,
"imagePath": "http://192.168.10.165:8080/f2.jpg",
"name": " Prawns 2",
"price": 12.5
} ]4) Example
//(2) take json Format string [] Convert to Java Object's List
private void jsonToJavaList() {
// 1 Get or create json
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imagePath\":
\"http://192.168.10.165:8080/f1.jpg\",\n" +
" \"name\": \" Prawns 1\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imagePath\":
\"http://192.168.10.165:8080/f2.jpg\",\n" +
" \"name\": \" Prawns 2\",\n" +
" \"price\": 12.5\n" +
" }\n" +
"]";
// 2 analysis json
Gson gson = new Gson();
List<ShopInfo> shops = gson.fromJson(json, new
TypeToken<List<ShopInfo>>() {
}.getType());
// 3 Show JSON data
tv_native_orignal.setText(json);
tv_native_last.setText(shops.toString());
}
4.2.3_ take Java Object to json character string {}
1) Use of API
String toJson(Object src);
2) Use steps
(1) take Gson Of jar Package import into project
(2) establish Gson object : Gson gson = new Gson();
(3) By creating Gson Object call toJson() Method , return json data :
ShopInfo shop = new ShopInfo(1, " Abalone ", 250.0, "");
String json = gson.toJson(shop);
3) Example
// (3) take Java Object to json character string {}
private void javaToJsonObject() {
// 1 Get or create Java object
ShopInfo shop = new ShopInfo(1, " Abalone ", 250.0, "");
// 2 Generate JSON data
Gson gson = new Gson();
String json = gson.toJson(shop);
// 3 Exhibition json data
tv_native_orignal.setText(shop.toString());
tv_native_last.setText(json);
}
4.2.3_ take Java Object's List Convert to json character string []
1) Use of API
String toJson(Object src);
2) Use steps
(1) take Gson Of jar Package import into project
(2) establish Gson object : Gson gson = new Gson();
(3) By creating Gson Object call toJson() Method , return json data :
List<ShopInfo> shops = new ArrayList<>();
String json = gson.toJson(shops);
3) Example
// (4) take Java Object's List Convert to json character string []
private void javaToJsonList() {
// 1 Get or create Java aggregate
List<ShopInfo> shops = new ArrayList<>();
ShopInfo baoyu = new ShopInfo(1, " Abalone ", 250, "baoyu");
ShopInfo haisen = new ShopInfo(2, " Sea cucumber ", 251, "haisen");
shops.add(baoyu);
shops.add(haisen);
// 2 Generate JSON data
Gson gson = new Gson();
String json = gson.toJson(shops);
// 3 Exhibition json data
tv_native_orignal.setText(shops.toString());
tv_native_last.setText(json);
}
4.3_FastJson Frame technology
1) characteristic :Fastjson It's a Java High performance and perfect function of language JSON library . It uses a “ Suppose there is
Order fast matching ” The algorithm of , hold JSON Parse To the extreme , Is currently the Java The fastest in the language JSON library .
2) Download address :https://github.com/alibaba/fastjson/wiki
4.3.1_ take json Format string {} Convert to Java object
1) Use of API
< T > T parseObject(String json, Class<T> classOfT); // take json Object to Java
Object method
Be careful : requirement json Object key The name of java The property name in the class corresponding to the object should be the same
2) Use steps
(1) Import fastjson Of jar package (2)JSON call parseObject() Method , Get the converted Java object
for example :ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
3) Test data
{
"id":2, "name":" Prawns ",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg" }4) Example
// (1) take json Format string {} Convert to Java object
private void jsonToJavaObjectByFastJson() {
// 1 Get or create JSON data
String json = "{\n" +
"\t\"id\":2, \"name\":\" Prawns \", \n" +
"\t\"price\":12.3, \n" +
"\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\ "\n" +
"}\n";
// 2 analysis JSON data
ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
// 3 Display the data
tv_fastjson_orignal.setText(json);
tv_fastjson_last.setText(shopInfo.toString());
}
4.3.2_ take json Format string [] Convert to Java Object's List
1) Use of API
List<T> parseArray(String json,Class<T> classOfT);// take json Array to Java Yes
Elephant list
Be careful : requirement json Object key The name of java The property name in the class corresponding to the object should be the same
2) Use steps
(1) Import fastjson Of jar package (2)JSON call parseArray() Method , Get the converted Java aggregate
for example :List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
3) Test data
[
{
"id": 1,
"imagePath": "http://192.168.10.165:8080/f1.jpg",
"name": " Prawns 1",
"price": 12.3
},
{
"id": 2,
"imagePath": "http://192.168.10.165:8080/f2.jpg",
"name": " Prawns 2",
"price": 12.5
} ]4) Example
// (2) take json Format string [] Convert to Java Object's List
private void jsonToJavaListByFastJson() {
// 1 Get or create JSON data
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imagePath\":
\"http://192.168.10.165:8080/f1.jpg\",\n" +
" \"name\": \" Prawns 1\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imagePath\":
\"http://192.168.10.165:8080/f2.jpg\",\n" +
" \"name\": \" Prawns 2\",\n" +
" \"price\": 12.5\n" +
" }\n" +
"]";
// 2 analysis JSON data
List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
// 3 Display the data
tv_fastjson_orignal.setText(json);
tv_fastjson_last.setText(shopInfos.toString());
}
4.3.3_ take Java Object to json character string {}
1) Use of API
String toJSONString(Object object);
2) Use steps
(1) Import fastjson Of jar package (2)JSON call toJSONString() Method , Get the converted json data
for example :
ShopInfo shopInfo = new ShopInfo(1, " Abalone ", 250.0, "baoyu");
String json = JSON.toJSONString(shopInfo);
3) Example
// (3) take Java Object to json character string {}
private void javaToJsonObjectByFastJson() {
// 1 obtain Java object
ShopInfo shopInfo = new ShopInfo(1, " Abalone ", 250.0, "baoyu");
// 2 Generate JSON data
String json = JSON.toJSONString(shopInfo);
// 3 data display
tv_fastjson_orignal.setText(shopInfo.toString());
tv_fastjson_last.setText(json);
}
4.3.4_ take Java Object's List Convert to json character string []
1) Use of API
String toJSONString(Object object);
2) Use steps
(1) Import fastjson Of jar package (2)JSON call toJSONString() Method , Get the converted json data
for example :
List<ShopInfo> shops = new ArrayList<>();
ShopInfo baoyu = new ShopInfo(1, " Abalone ", 250.0, "baoyu");
ShopInfo longxia = new ShopInfo(2, " lobster ", 251.0, "longxia");
shops.add(baoyu);
shops.add(longxia);
String json = JSON.toJSONString(shops);
3) Example
// (4) take Java Object's List Convert to json character string []
private void javaToJsonArrayByFastJson() {
// 1 obtain Java aggregate
List<ShopInfo> shops = new ArrayList<>();
ShopInfo baoyu = new ShopInfo(1, " Abalone ", 250.0, "baoyu");
ShopInfo longxia = new ShopInfo(2, " lobster ", 251.0, "longxia");
shops.add(baoyu);
shops.add(longxia);
// 2 Generate JSON data
String json = JSON.toJSONString(shops);
// 3 data display
tv_fastjson_orignal.setText(shops.toString());
tv_fastjson_last.setText(json);
}
5_ Tool use
5.1_GsonFormat
1) open Android studio page , Click the setup button .
2) Click on Plugins Button
3) Enter... In the input box on the right gsonformat, Then click... In the middle Browse( You have to click... When you are connected to the Internet ) 4) choice GsonFormat, Click Install plugin on the right
5) Restart it Android studio
6) stay Android studio Create a class
7) Press... In this class at the same time alt+shift+s, And click the GsonFormat
8) To be resolved JSON Paste the string into GsonFormat in
9) Click on OK
10) This will input JSON Data is converted to bean object
public class TestGsonFormat {
/**
* id : 2
* name : Prawns
* price : 12.3
* imagePath : http://192.168.10.165:8080/L05_Server/images/f1.jpg
*/
private int id;
private String name;
private double price;
private String 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;
} }
5.2_HiJson download
http://www.downza.cn/soft/209370.html
1) Double click the icon
2) To be resolved JSON Paste the data into the left page
3) Click Format JSON character string , It will be convenient to view on the right JSON data
边栏推荐
- Insert pictures in word to maintain high DPI method
- Configuration and application of gurobi in pycharm
- How PHP changes a two-dimensional array into a one-dimensional array
- Cron expression use
- Some queries of TP5
- DRF learning notes (preparation)
- Notes on implementation and acquisition of flowable custom attributes
- 收藏!0基础开源数据可视化平台FlyFish大屏开发指南
- 2021-06-02
- What is ti's calculation for successively canceling the agency rights of anfuli / Wenye / Shiping?
猜你喜欢

Opencv (V) -- moving target recognition

Reduce PDF document size (Reprint)

OpenCV(三)——图像分割

SolidWorks Simulation曲线图属性设定
![[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images](/img/7d/a80f216117b647abac414fd82821da.png)
[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images

Log management

The image displayed online by TP5 is garbled

Leetcode25 question: turn the linked list in a group of K -- detailed explanation of the difficult questions of the linked list

Draw circuit diagram according to Verilog code

Opencv (II) -- basic image processing
随机推荐
Collection! 0 basic open source data visualization platform flyfish large screen development guide
codis集群部署
DRF learning notes (I): Data Serialization
Opencv (II) -- basic image processing
Script file ‘D:\programs\anaconda3\Scripts\pip-script.py‘ is not present.
[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images
The class declared by the scala object keyword directly calls methods and associated objects
Polynomial locus of order 5
Google Chrome reversecaptcha ad blocking
2021-06-02
闲聊技巧
excel skill
OpenCV(四)——图像特征与目标检测
DRF use: get request to get data (small example)
[cqoi2012] local minima & Mike and foam
How PHP changes a two-dimensional array into a one-dimensional array
Exe program encryption lock
2021-06-02
重新配置cubemx后,生成的代码用IAR打开不成功
Solve the problem that ${pagecontext.request.contextpath} is invalid