Android完全符合規則但很頭疼的Json映射成一個樹結構且可折疊的清單?

前言
前些天有個朋友問我,要實現一個樹狀的清單要怎麼做,根據一個完全符合規則但是卻很頭疼的一個Json解析來實現,見下格式,對於有些Android開發者來說,這個Json或許並不友好,沒有辦法直接轉成實體類,其實這一串Json解析映射成可折疊清單也並不難!
程式碼已提交至Github:http://www.bankingforeign.com
{
“code”:“200”,
“message”:“success”,
“data”:[
{
“id”:“1001”,
“title”:“編號1”,
“next”:[
{
“id”:“10011”,
“title”:“編號1-1”
},
{
“id”:“10012”,
“title”:“編號1-2”,
“next”:[
{
“id”:“100121”,
“title”:“編號1-2-1”,
“next”:[
{
“id”:“1001211”,
“title”:“編號1-2-1-1”
},
{
“id”:“1001212”,
“title”:“編號1-2-1-2”
},
{
“id”:“1001213”,
“title”:“編號1-2-1-3”
},
{
“id”:“1001214”,
“title”:“編號1-2-1-4”
},
{
“id”:“1001215”,
“title”:“編號1-2-1-5”
}
]
},
{
“id”:“100122”,
“title”:“編號1-2-2”
},
{
“id”:“100123”,
“title”:“編號1-2-3”,
“next”:[
{
“id”:“1001231”,
“title”:“編號1-2-3-1”
},
{
“id”:“1001232”,
“title”:“編號1-2-3-2”
},
{
“id”:“1001233”,
“title”:“編號1-2-3-3”
},
{
“id”:“1001234”,
“title”:“編號1-2-3-4”
},
{
“id”:“1001235”,
“title”:“編號1-2-3-5”
}
]
}
]
},
{
“id”:“10013”,
“title”:“編號1-3”
}
]
},
{
“id”:“1002”,
“title”:“編號2”
},
{
“id”:“1003”,
“title”:“編號3”
},
{
“id”:“1004”,
“title”:“編號4”,
“next”:[
{
“id”:“10041”,
“title”:“編號4-1”
},
{
“id”:“10042”,
“title”:“編號4-2”
}
]
},
{
“id”:“1005”,
“title”:“編號5”
}
]
}

拿到這一串不確定層級的Json該想什麼?用什麼去解析?該用什麼控制項?
逐層addView管道
其實可以直接使用Gson解析,不過這個實體類要自己手寫一下:
packagecom.example.myapplication;
importjava.util.List;
public class DataBean {
private String code;
private String message;
private List<Data> data;
public String getCode(){
return code;
}
public void setCode(String code){
this.code= code;
}
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message= message;
}
public List<Data> getData(){
return data;
}
public void setData(List<Data> data){
this.data= data;
}
public static class Data {
private String id;
private String title;
private List<Data> next;//重點在這裡
public String getId(){
return id;
}
public void setId(String id){
this.id= id;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title= title;
}
public List<Data> getNext(){
return next;
}
public void setNext(List<Data> next){
this.next= next;
}
}
}
(OpenParam.json為那個json字串)
使用Gson解析:
Kotlin:
val dataBean = Gson().fromJson(OpenParam.json,DataBean().javaClass)
Java:
DataBean dataBean = new Gson().fromJson(OpenParam.json,DataBean.class)
既然解析出來了,之後可以通過遞迴來逐漸addView()的管道實現,判斷next欄位是否為null即可!但要在遞迴開始之前,先要分析一下佈局!
既然要逐級嵌套,先來一個LinearLayout,當然這個清單是可滑動的,外層嵌套一個ScrollView即可,Activity佈局那就是這樣的:
<?xml version=“1.0”encoding=“utf-8”?>br/><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android=“http://schemas.android.com/apk/res/android“
xmlns:tools=“http://schemas.android.com/tools“
android:layout_width=“match_parent“
android:layout_height=“match_parent“
tools:context=“.MainActivity”>
<ScrollView
android:layout_width=“match_parent“
android:layout_height=“wrap_content“
tools:ignore=“MissingConstraints”>
<LinearLayout
android:id=“@+id/treeLayout”
android:layout_width=“match_parent“
android:orientation=“vertical”
android:layout_height=“wrap_content“>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

猜你喜欢

转载自blog.51cto.com/15002732/2609187