kotlin的Gson的使用
首先你得先加入依賴庫 ‘com.google.code.gson:gson:2.8.5’
一:沒有資料頭的純屬組Json解析
[
{
"name" : "zhangsan" ,
"age" : "10" ,
"phone" : "11111" ,
"email" : "11111@11.com"
} ,
{
"name" : "lisi" ,
"age" : "20" ,
"phone" : "22222" ,
"email" : "22222@22.com"
} ,
...
]
1 .需要一個Bean物件。
data class UserBean (
val name: String,
val age: String,
val phone: Strin,
val emall: String,
)
2.開始解析 只需要3部分
fun parseNoHeaderJArray ( ) {
val userJson= UserApi. instance. getOriginalFunData ( this )
val userJson= JsonToStringUtil. getStringByJson ( this , R. raw. juser_1)
val parser= JsonParser ( )
val JsonArray= parser. parser ( userJson) . asJsonArray
val gson= Gson ( )
val userList= mutableListOf< UserBean> ( )
for ( user : jsonArray) {
val userBean= gson. fromJson ( user, UserBean:: class . java)
userList. add ( userBean)
}
println ( userList)
}
二:有資料頭的純資料陣列資料解析
{
"muser" : [
{
"name" : "zhangsan" ,
"age" : "10" ,
"phone" : "11111" ,
"email" : "11111@11.com"
} ,
{
"name" : "lisi" ,
"age" : "20" ,
"phone" : "22222" ,
"email" : "22222@22.com"
} ,
...
]
}
解析過程
fun parseHaveHeaderJArray ( ) {
val cuerJson = CityApi. instance. getOriginalFundData ( this )
val jsonObject = sonParser ( ) . parse ( userJson) . asJsonObject
val jsonArray = jsonObject. getAsJsonArray ( "muser" )
val gson = Gson ( )
val userList = mutableListOf< UserBean> ( )
for ( user in jsonArray) {
val userBean =
gson. fromJson< UserBean> (
user,
object : TypeToken< UserBean> ( ) { } . type)
userList. add ( userBean)
}
print ( userList)
}
總結:
1.與第一種類似,不同的是在獲取到JsonArrary後
再從JsonArrary中拿到key:muser的值getAsJsonArray(“muser”)
2.在解析資料的時候通過TypeToken反射拿到資料類 這是一個匿名內部類
作用就是獲取像list這樣的泛型資訊。自行腦補
三:有資料頭的複雜數
{
"code" : 200 ,
"msg" : "OK" ,
"muser" : [
{
"name" : "zhangsan" ,
"age" : "10" ,
"phone" : "11111" ,
"email" : "11111@11.com"
} ,
{
"name" : "lisi" ,
"age" : "20" ,
"phone" : "22222" ,
"email" : "22222@22.com"
} ,
...
]
}
根據json的資料格式建立實體類,這裡的實體類是包含所有欄位,Gson能直接解析成List
so:Bean這樣寫 由兩個data類組成 如果UserBean中還有List 同樣建立一個OtherBean data類 所有的類都可以寫在一個.kt檔案中
data class ResultBean (
val code: Int,
val msg: String,
val muser: List< UserBean>
)
data class UserBean (
val name: String,
val age: String,
val phone: String,
val email: String
)
現在看一下Gson是如何解析的把
fun parseHaveHeaderJArray ( ) {
val resultJson= CityApi. instance. getOriginalFundData ( this )
val resultBean=
Gson ( ) . fromJson ( resultJson, ResultBean:: class . java)
val userBeanList= resultBean. muser
print ( userBeanList)
}
這種方式是最簡單的
這幾種方式差不多能應對所有的Json的資料型別了
還有其他內容更多更復雜的資料就不一一列舉了。
如和從**.json檔案中獲取資料:
從assets資料夾中獲取.json的檔案轉換成字串
class UserApi private constructor ( ) {
companion object {
val instance = UserApi ( )
}
fun getOriginalFundData ( context: Context) : String? {
var inputStream: InputStream? = null
try {
inputStream = context. assets. open ( "user.json" )
return convertStreamToString ( inputStream) as String
} catch ( e: Exception) {
e. printStackTrace ( )
}
return null
}
fun convertStreamToString ( input: InputStream) : String? {
var s: String? = null
try {
val scanner = Scanner ( input, "UTF-8" )
. useDelimiter ( "\\A" )
if ( scanner. hasNext ( ) ) {
s = scanner. next ( )
}
input. close ( )
} catch ( e: IOException) {
e. printStackTrace ( )
}
return s
}
}