英文:
Remove dublicates value from json array in android
问题
[
{
"product_name": "13X25",
"ask_size": 0,
"product_id": "5",
"category_id": "1",
"quantity": "10",
"image": null,
"description": "",
"product_pdf": null,
"head": "MARSHAL",
"price": "22.00",
"user_quantity": "2"
},
{
"product_name": "14X25",
"ask_size": 0,
"product_id": "6",
"category_id": "1",
"quantity": "12",
"image": null,
"description": "",
"product_pdf": null,
"head": "MARSHAL",
"price": "23.00",
"user_quantity": "1"
},
{
"product_name": "14X25 C",
"ask_size": 0,
"product_id": "2",
"category_id": "1",
"quantity": "23",
"image": null,
"description": "",
"product_pdf": null,
"head": "KANGARO",
"price": "22.00",
"user_quantity": "0"
},
{
"product_name": "17X25 C",
"ask_size": 0,
"product_id": "1",
"category_id": "1",
"quantity": "18",
"image": null,
"description": "",
"product_pdf": null,
"head": "HOKO",
"price": "12.00",
"user_quantity": "0"
}
]
(Note: I have provided the translated JSON data as requested. If you need further assistance on how to achieve the task in Android or using a specific programming language, please feel free to ask.)
英文:
[{"product_name":"13X25","ask_size":0,"product_id":"5","category_id":"1","quantity":"10","image":null,"description":"","product_pdf":null,"head":"MARSHAL","price":"22.00","user_quantity":"2"},{"product_name":"14X25","ask_size":0,"product_id":"6","category_id":"1","quantity":"12","image":null,"description":"","product_pdf":null,"head":"MARSHAL","price":"23.00","user_quantity":"1"},{"product_name":"14X25 C","ask_size":0,"product_id":"2","category_id":"1","quantity":"23","image":null,"description":"","product_pdf":null,"head":"KANGARO","price":"22.00","user_quantity":"0"},{"product_name":"17X25 C","ask_size":0,"product_id":"1","category_id":"1","quantity":"18","image":null,"description":"","product_pdf":null,"head":"HOKO","price":"12.00","user_quantity":"0"}]
This is my JSON array
Where I want to remove a particular key if it is already exist
There is a key named "head": "MARSHAL", This Key comes two times but I want to fetch it only one time.
If it's coming then it must be removed automatically from other JSONObject, Not only for one key
it can be to other keys also if they get repeated then I want them only one time and after that add that JSON data to the list
How can I do it for android?
I want to show this data into cardView adapter
答案1
得分: 2
首先,将你的JSON转换为List<Map<String, Object>>。你可以使用ObjectMapper来实现。
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class Test {
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
List<Map<String, String>> listOfMap = createListMap();
Map<String, List<String>> tempMap = new HashMap<>();
listOfMap.forEach(
map -> {
Set<String> keySet = new HashSet<>(map.keySet());
keySet.forEach(
key -> {
List<String> tempValue = tempMap.get(key);
if (Objects.nonNull(tempValue) && tempValue.contains(map.get(key))) {
map.remove(key);
} else {
tempMap.put(key, Arrays.asList(map.get(key)));
}
});
});
System.out.println(createListMap(listOfMap));
}
private static List<Map<String, String>> createListMap() throws IOException {
File jsonFile = new File("src/main/resources/input.json");
return mapper.readValue(jsonFile, new TypeReference<List<Map<String, String>>>() {});
}
private static String createListMap(List<Map<String, String>> listOfMap) throws IOException {
return mapper.writeValueAsString(listOfMap);
}
}
以下是你的输入JSON的输出结果:
[
{
"product_name": "13X25",
"ask_size": "0",
"product_id": "5",
"category_id": "1",
"quantity": "10",
"image": null,
"description": "",
"product_pdf": null,
"head": "MARSHAL",
"price": "22.00",
"user_quantity": "2"
},
{
"product_name": "14X25",
"product_id": "6",
"quantity": "12",
"price": "23.00",
"user_quantity": "1"
},
{
"product_name": "14X25 C",
"product_id": "2",
"quantity": "23",
"head": "KANGARO",
"price": "22.00",
"user_quantity": "0"
},
{
"product_name": "17X25 C",
"product_id": "1",
"quantity": "18",
"head": "HOKO",
"price": "12.00"
}
]
希望这对你有所帮助。
英文:
First of all, convert your JSON to List<Map<String, Object>>.
You can use ObhectMapper to do so.
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class Test {
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
List<Map<String, String>> listOfMap = createListMap();
Map<String, List<String>> tempMap = new HashMap<>();
listOfMap.forEach(
map -> {
Set<String> keySet = new HashSet<>(map.keySet());
keySet.forEach(
key -> {
List<String> tempValue = tempMap.get(key);
if (Objects.nonNull(tempValue) && tempValue.contains(map.get(key))) {
map.remove(key);
} else {
tempMap.put(key, Arrays.asList(map.get(key)));
}
});
});
System.out.println(createListMap(listOfMap));
}
private static List<Map<String, String>> createListMap() throws IOException {
File jsonFile = new File("src/main/resources/input.json");
return mapper.readValue(jsonFile, new TypeReference<List<Map<String, String>>>() {});
}
private static String createListMap(List<Map<String, String>> listOfMap) throws IOException {
return mapper.writeValueAsString(listOfMap);
}
}
Below is the output for your input JSON.
> [{"product_name":"13X25","ask_size":"0","product_id":"5","category_id":"1","quantity":"10","image":null,"description":"","product_pdf":null,"head":"MARSHAL","price":"22.00","user_quantity":"2"},{"product_name":"14X25","product_id":"6","quantity":"12","price":"23.00","user_quantity":"1"},{"product_name":"14X25
> C","product_id":"2","quantity":"23","head":"KANGARO","price":"22.00","user_quantity":"0"},{"product_name":"17X25
> C","product_id":"1","quantity":"18","head":"HOKO","price":"12.00"}]
May this serves your purpose.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论