问题描述
我有这个JSON数据
{"mydata":{"numbers":[12,23,34]}}
通过使用JSONObject,我得到以下对象:
By taking JSONObject I get following object:
JSONObject obj = mydataObj.getJSONObject("mydata");
所以obj是这样的:
{"numbers":[12,23,34]}
我需要使用数字元素.因此,我将进行for循环以使用元素.
I need to use the numbers elements. So I will make a for-loop to use elements.
String numbers = myObject.getString("numbers");
这给出了这样的字符串:
This gives a string like this:
["12","23","34"]
如何将其转换为数组或arraylist?
因此,我将得到一个值分别为12、23和34的数组.
How can I convert this to an array or arraylist?
So I will have an array having values of 12, 23 and 34.
获取JSONArray的Array,ArrayList之后,我将比较"numbers1_Array"和"numbers2_Array".然后,我将找到第一个数组中存在但第二个数组中不存在的元素.因此,选择与哪种比较操作相关的数据结构类型更加容易.
After getting the Array, ArrayList of JSONArray, I will compare "numbers1_Array" and "numbers2_Array". Then I will find the elements that exists in first array but doesn't exists in second array. So selecting data structure type related to which compare operation is easier.
推荐答案
您可以尝试 getJSONArray()
而不是getString()
,然后只需遍历 JSONArray
并构建您的ArrayList
.
You could try getJSONArray()
instead of getString()
, and then simply iterate over the JSONArray
and build your ArrayList
.
这篇关于将JSONArray字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!