不使用GSON。
如果myFilterObj对象中的“chore”数组中有匹配的值,我将尝试从"chores"数组中提取文本值。
{
"chores": [
{
"text": "Wash the car",
"value": 1
},
{
"text": "Mow the lawn",
"value": 2
},
{
"text": "Vaccumm the floor",
"value": 3
},
{
"text": "Walk the dog",
"value": 4
}
],
"myFilterObj": {
"chore": [
1,
2
]
}
}
字符串
我做的第一件事就是把它们转换成数组列表。
下面是两个ArrayList。我的目标是如果第一个ArrayList中的值在第二个ArrayList中,则将第一个ArrayList中的文本重新排序。
例如:洗车和Vibrum地板应该是空的,因为1和2在第二个数组列表中,1和2是第一个数组列表中的值。
这是我目前为止所做的。我一直在尝试对allChores.value进行比较。我想我应该使用removeAll或contains??
JSONArray allChores = data.getJSONArray("chores");
JSONArray myChores = myFilterObj.getJSONArray("chore");
ArrayList<Object> listdata1 = new ArrayList<Object>();
ArrayList<Object> listdata2 = new ArrayList<Object>();
if (allChores != null) {
for (int i = 0; i < allChores.length(); i++) {
//Adding each element of JSON array into ArrayList
listdata1.add(allChores.get(i));
}
}
if (myChores != null) {
for (int i = 0; i < myChores.length(); i++) {
//Adding each element of JSON array into ArrayList
listdata2.add(myChores.get(i));
}
}
//comapare allChores.value to myChores(value)
//printout allChores.text on match
[
{
"text":"Wash the car",
"value":1
},
{
"text":"Mow the lawn",
"value":7
},
{
"text":"Vaccumm the floor",
"value":2
},
{
"text":"Walk the dog",
"value":8
}
]
[
1,
2,
3
]
2条答案
按热度按时间klr1opcd1#
下面的答案是在假设可以使用GSON的情况下编写的。
首先,我会将输入转换为POJO。为此,我会为输入中的每个对象引入单独的记录
Chore
,McChores
和Input
本身。字符串
这样你就可以创建查找Map的家务
型
导致
型
8yoxcaq72#
有点矫枉过正,但我的首选解决方案是为这些杂务创建一个查找Map,然后遍历ID以生成标签。
字符串
然后,输入的处理或多或少与我的GSON解决方案相同,但更多的过程处理,因为JSONArays已经是一个Iterable,否则它需要转换两次(Iterable -> List -> Stream)。
型
这也导致
型