我正在尝试解析数组,我尝试在JsonArray中获取数组,但失败了。
String myJson="{"type_history":[["15","0.07","apple"],["13","0.03","banana"],["10","0.23","lemon"]]}";
ArrayList<UserType> rtn = new ArrayList<>();
JSONArray j=myJson.optJSONArray("type_history");
for (int i = 0; i < j.length(); i++) {
UserType tmp = new UserType();
JSONArray tmpArray = new JSONArray(Arrays.asList(j.get(i)));
tmp.setType(tmpArray.getString(0));
tmp.setValue(Float.parseFloat(tmpArray.getString(1)));
rtn.add(tmp);
}
在tmpArray.getString(0)
中,我仍然得到["15","0.07","apple"]
,而不是"15"
如何修复我的代码,以便从这个数组中获取值?
发布于 2019-06-27 04:09:40
我们开始吧:
var listStr: JSONArray? = null
val json = "{type_history:[[15,0.07,apple],[13,0.03,banana],[10,0.23,lemon]]}"
val jsonObject = JSONObject(json)
val jsonArray = jsonObject.getJSONArray("type_history")
for(i in 0 until jsonArray.length()){
listStr = jsonArray.get(i) as JSONArray
for(x in 0 until listStr.length()) {
Log.d("mlogs", listStr.get(x).toString())
}
}
https://stackoverflow.com/questions/56779580
复制相似问题