简体   繁体   中英

How can I get data from a JSON file?

Hello I have a problem to get the data from a JSON file. Actually, before I had this format :

{"country":"US","Money":"Dollars US"}

And to get the result I did this in my inner class by the name of AsyncTask :

override fun onProgressUpdate(vararg values: String?) {
        try{
            var json = JSONObject(values[0])
            var country = json.getString("country")
            test1.text = "Country is "+country
            }catch(ex:Exception){}
    }

But now the format is different I have this :

["US", "Dollars US"]

And I have no idea how to modify my AsyncTask class to get the data ?

Thank you for your help !

Your question seems little unclear to me, but assuming this is the only data in the json file

["US", "Dollars US"]

you should update your function like this:

override fun onProgressUpdate(vararg values: String?) {
    try{
        var json = JSONArray(values[0])  //since it is a array now and not a object
        var country = json.getString(0)  //Oth position of the JSON array will give you the country name
        test1.text = "Country is "+country
        }catch(ex:Exception){}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM