Android JSON parsing Kotlin

Source: Internet
Author: User

1. First tell me what is json--the following is the JSON basic format, is actually a key value pair.

{"People""firstName": "Brett", "LastName": "McLaughlin", "email": "AAAA""firstName": "Jason", " LastName ":" Hunter "," email ":" bbbb "" firstName ":" Elliotte "," LastName ":" Harold "," email ":" CCCC " }]} 



2.

 PackageUtilImportCom.example.jason_jan.guangdamiao.bean.PersonImportOrg.json.JSONArrayImportorg.json.JSONExceptionImportOrg.json.JSONObjectImportjava.util.ArrayListImportJava.util.HashMap/*** Created by Jason_jan on 2017/7/5.*/classJsonutil {Companion object {/*** Get Object Data *@paramKey * * *@paramjsonstring * * *@return         */Fun Getperson (key:string, jsonstring:string): person {val person=Person ()Try{val Jsonobject=Jsonobject (jsonstring) Val personobject=Jsonobject.getjsonobject (key) person . UserID = personobject.getstring ("UserID" )//See situation modification here } Catch(e:jsonexception) {e.printstacktrace ()}returnPerson }/*** Get Object array Data *@paramKey * * *@paramjsonstring * * *@return         */Fun getpersons (key:string, jsonstring:string): List<Person>{val list= arraylist<person>()            Try{val Jsonobject=Jsonobject (jsonstring)//returns an array of JSONVal Jsonarray =Jsonobject.getjsonarray (Key) for(I in 0..jsonarray.length ()-1) {val JsonObject2=Jsonarray.getjsonobject (i) val  person=Person  ()//here to see the situation modified List.add (person) }}                     Catch(e:jsonexception) {e.printstacktrace ()}returnList}/*** Get string array data *@paramKey * * *@paramjsonstring * * *@return         */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 ()}returnList}/*** Get the Map collection data of the object *@paramKey * * *@paramjsonstring * * *@return         */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 ()}returnlist} }}

 2. How to use it is very simple. Called in handler

As Var person:person--here declares a person class

Person=jsonutil.getperson ("JSON returned keywords", content obtained in httputil)

Txt1.text=person.tostring ()--txt1 is a textview in the layout 


3. In fact, JSON parsing is usually linked to HTTP, carefully pondering, you will reap a lot.

  

Android JSON parsing Kotlin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.