前面 写了一次 kotlin解析json 但是,真的写得太烂,直接删掉了,现在重新整理一下。顺便记录一下今天坑了我很久的小问题。
1.首先从最简单的入手吧
一个json的字符串:=====就叫做jsonString吧
{ "code": "200", "msg": "success", "debugMsg": "", "result": { "data": "1231414" } }
好了怎么解析呢???
用一个函数即可
fun get_key_string(key:String,jsonString:String):String { var str: String = "" try { val jsonObj:JSONObject= JSONObject(jsonString) str=jsonObj.getString(key) } catch (e: JSONException) { e.printStackTrace() } return str }
怎么用呢???
val msg=get_key_string("msg","jsonString")
第一个参数=====在json字符串中,你想要解析的key,我上面的例子有 code,msg,debugMsg,result,我选择的是msg
第二个参数=====就是json字符串的名称,就是一个字符串,不要想得太复杂了
msg解析后的结果=====success
2.多重解析
依旧是上面的例子吧=====json字符串名字依旧叫做jsonString
{ "code": "200", "msg": "success", "debugMsg": "", "result": { "data": "1231414" } }
我想要得到data怎么办呢???
很简单,两步即可。
val result=get_key_String("result","jsonString")//=====这里的第二个参数依旧是jsonString
val data=get_key_String("data","result")//=====注意这里的第二个参数已经变成了result了
3.解析整型,boolean类型
这个和解析String类型类似,直接上代码吧
fun get_key_boolean(key:String,jsonString:String):Boolean {//解析成Boolean var str: Boolean =true try { val jsonObj:JSONObject= JSONObject(jsonString) str=jsonObj.getBoolean(key) } catch (e: JSONException) { e.printStackTrace() } return str } fun get_key_int(key:String,jsonString:String):Int {//解析成Int var str: Int=0 try { val jsonObj:JSONObject= JSONObject(jsonString) str=jsonObj.getInt(key) } catch (e: JSONException) { e.printStackTrace() } return str }
4.稍微牛逼一点的解析=====解析成List<Map<String,Any>>=====其实这个就比较通用了,很多东西直接用这个就可以了
比如来一个复杂一点的jsonString吧
{ "code": "200", "msg": "success", "debugMsg": "", "result": { "data": [ "key1": "string" "key2": 123 "key3": true
] }}
这样一个jsonString,key1对应一个字符串,key2对应一个整型,key3对应一个布尔类型
怎么解析呢???
fun getListMap(key: String, jsonString: String): List<Map<String, Any>> { val list = ArrayList<Map<String, Any>>() try { val jsonObject = JSONObject(jsonString) val jsonArray = jsonObject.getJSONArray(key) for (i in 0..jsonArray.length() - 1) { val jsonObject2 = jsonArray.getJSONObject(i) val map = HashMap<String, Any>() val iterator = jsonObject2.keys() while (iterator.hasNext()) { val json_key = iterator.next() var json_value: Any? = jsonObject2.get(json_key) if (json_value == null) { json_value = "" } map.put(json_key, json_value) } list.add(map) } } catch (e: JSONException) { e.printStackTrace() } return list }
主要有2个步骤
val result=get_key_string("result","jsonString")
val data=getListMap("data","result")
就这么666
5.将所有的可以用到的方法封装起来吧
class JsonUtil { companion object { fun get_key_string(key:String,jsonString:String):String { var str: String = "" try { val jsonObj:JSONObject= JSONObject(jsonString) str=jsonObj.getString(key) } catch (e: JSONException) { e.printStackTrace() } return str } fun get_key_boolean(key:String,jsonString:String):Boolean { var str: Boolean =true try { val jsonObj:JSONObject= JSONObject(jsonString) str=jsonObj.getBoolean(key) } catch (e: JSONException) { e.printStackTrace() } return str } fun get_key_int(key:String,jsonString:String):Int { var str: Int=0 try { val jsonObj:JSONObject= JSONObject(jsonString) str=jsonObj.getInt(key) } catch (e: JSONException) { e.printStackTrace() } return str } fun getList(key: String, jsonString: String): List<String> { val list = ArrayList<String>() try { val jsonObject = JSONObject(jsonString) val jsonArray = jsonObject.getJSONArray(key) for (i in 0..jsonArray.length() - 1) { val msg = jsonArray.getString(i) list.add(msg) } } catch (e: JSONException) { e.printStackTrace() } return list } fun getListMap(key: String, jsonString: String): List<Map<String, Any>> { val list = ArrayList<Map<String, Any>>() try { val jsonObject = JSONObject(jsonString) val jsonArray = jsonObject.getJSONArray(key) for (i in 0..jsonArray.length() - 1) { val jsonObject2 = jsonArray.getJSONObject(i) val map = HashMap<String, Any>() val iterator = jsonObject2.keys() while (iterator.hasNext()) { val json_key = iterator.next() var json_value: Any? = jsonObject2.get(json_key) if (json_value == null) { json_value = "" } map.put(json_key, json_value) } list.add(map) } } catch (e: JSONException) { e.printStackTrace() } return list } fun get_ask_Item(key:String,jsonString:String): ArrayList<MyAsk>? { var myAsk=ArrayList<MyAsk>() try{ val jsonObject=JSONObject(jsonString) val jsonArray=jsonObject.getJSONArray(key) for(i in 0..jsonArray.length()-1){ val json_ask=jsonArray.getJSONObject(i) var my_ask=MyAsk() val commentCount=json_ask.getString("commentCount") val commentID=json_ask.getString("commentID") val content=json_ask.getString("content") val createTime=json_ask.getString("createTime") val gender=json_ask.getString("gender") val icon=json_ask.getString("icon") val id=json_ask.getString("id") val isFavorite=json_ask.getString("isFavorite") val isZan=json_ask.getString("isZan") val label=json_ask.getString("label") val link=json_ask.getString("link") val nickname=json_ask.getString("nickname") val publisherID=json_ask.getString("publisherID") val reward=json_ask.getString("reward") val type=json_ask.getString("type") val zan=json_ask.getString("zan") val image_array=json_ask.getJSONArray("image") var image_list:ArrayList<String>?=null for(j1 in 0..image_array.length()-1){ val image_item=image_array.getString(j1) image_list?.add(image_item) } val thumbnailImage_array=json_ask.getJSONArray("thumbnailImage") var image_list2:ArrayList<String>?=null for(j2 in 0..thumbnailImage_array.length()-1){ val image_item=thumbnailImage_array.getString(j2) image_list2?.add(image_item) } my_ask.commentID=commentID my_ask.commentCount=commentCount my_ask.content=content my_ask.createTime=createTime my_ask.gender=gender my_ask.icon=icon my_ask.id=id my_ask.favoriteIs=isFavorite my_ask.zan=zan my_ask.zanIs=isZan my_ask.label=label my_ask.link=link my_ask.nickname=nickname my_ask.publisherID=publisherID my_ask.reward=reward my_ask.type=type my_ask.image=image_list my_ask.thumbnailImage=image_list2 myAsk?.add(my_ask) } return myAsk }catch (e:JSONException){ e.printStackTrace() } return myAsk } } }
最后一种是自定义式解析,因为项目中的jsonString可以非常复杂,所以要自己定义一个类,然后用类似的方法来解析即可。
因为我博客名也叫做Jason_Jan对于json还是比较情有独钟的,可以说json真的很棒
推荐两个第三方库
Ason=====github链接:https://github.com/afollestad/ason
GsonFormat=====github链接:https://github.com/zzz40500/GsonFormat