您好,我在从 JSON 文件获取数据时遇到问题。实际上,在我有这种格式之前:
{"country":"US","Money":"Dollars US"}
为了得到结果,我在我的内部类中以 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){}
}
但现在格式不同了,我有这个:
["US", "Dollars US"]
而且我不知道如何修改我的 AsyncTask 类来获取数据?
感谢您的帮助!
最佳答案
你的问题对我来说似乎有点不清楚,但假设这是 json 文件中的唯一数据
["US", "Dollars US"]
你应该像这样更新你的函数:
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){}
}
关于android - 如何从 JSON 文件中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505987/