从 JSON 数组中移除重复的值在安卓中。

huangapple go评论74阅读模式
英文:

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.)

英文:

从 JSON 数组中移除重复的值在安卓中。 从 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","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&lt;Map&lt;String, String&gt;&gt; listOfMap = createListMap();
Map&lt;String, List&lt;String&gt;&gt; tempMap = new HashMap&lt;&gt;();
listOfMap.forEach(
map -&gt; {
Set&lt;String&gt; keySet = new HashSet&lt;&gt;(map.keySet());
keySet.forEach(
key -&gt; {
List&lt;String&gt; tempValue = tempMap.get(key);
if (Objects.nonNull(tempValue) &amp;&amp; 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&lt;Map&lt;String, String&gt;&gt; createListMap() throws IOException {
File jsonFile = new File(&quot;src/main/resources/input.json&quot;);
return mapper.readValue(jsonFile, new TypeReference&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;() {});
}
private static String createListMap(List&lt;Map&lt;String, String&gt;&gt; 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.

huangapple
  • 本文由 发表于 2020年9月30日 01:15:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64124506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定