1

Following Json needs to group by kecamatan to get array of desa along with count if kecamatan have same array value.

{
"data": [
        {
            "ID": 47,
            "kecamatan": "Benteng",
            "desa": "Benteng Selatan"
        },
        {
            "ID": 48,
            "kecamatan": "Benteng",
            "desa": "Benteng Selatan"
        },
        {
           "ID": 49,
            "kecamatan": "Benteng",
            "desa": "Benteng Utara"
        },
        {
            "ID": 50,
            "kecamatan": "Bontomantene",
            "desa": "Garaupa"
        }
    ]
}

expected output :-

  • Benteng [Benteng selatan] = 2
  • Benteng [Benteng Utara] = 1
  • Bontomantene [Garaupa] = 1
4
  • 1
    what yo have tried so far? Commented Nov 14, 2019 at 5:16
  • i already parsing with volley and only add into array list Commented Nov 14, 2019 at 5:30
  • what does 2 and 1 mean ? Commented Nov 14, 2019 at 6:04
  • json array have same value Commented Nov 14, 2019 at 6:19

2 Answers 2

2

Your question can be considered as "Count the number of occurrences of an element in a list grouping by fields kecamatan and desa".
Therefore, if you can convert the given JSON string to a list of object such as a List<Data>, then you can count the number of occurrences with Stream and Lambda Expression (since Java 8) as follows:

POJOs

class MyDatas {
    private List<MyData> data;

    //general getters and setters
}

@JsonIgnoreProperties(ignoreUnknown = true)
class MyData {
    private String kecamatan;
    private String desa;

    public String getGroupBy() {
        return String.format("%s [%s]", this.getKecamatan(), this.getDesa());
    }

    //general getters and setters
}

For class MyData, I create a method getGroupBy() which return the key for grouping.

Code snippet

ObjectMapper mapper = new ObjectMapper();
MyDatas myDatas = mapper.readValue(jsonStr, MyDatas.class);
Map<String, Long> counting = myDatas.getData()
        .stream()
        .collect(Collectors.groupingBy(
                MyData::getGroupBy, Collectors.counting()));
System.out.println(counting);

Use Jackson to convert the JSON string to POJO first, then use Collectors.groupingBy() to count the number of occurrences with MyData::getGroupBy.

Console output

{Bontomantene [Garaupa]=1, Benteng [Benteng Selatan]=2, Benteng [Benteng Utara]=1}

Sign up to request clarification or add additional context in comments.

Comments

1

Please try this

private void jsonObject() {
    String result =  "{\"data\": [{\"ID\": 47,\"kecamatan\": \"Benteng\",\"desa\": \"Benteng Selatan\"},{\"ID\": 48,\"kecamatan\": \"Benteng\",\"desa\": \"Benteng Selatan\"},{\"ID\": 49,\"kecamatan\": \"Benteng\",\"desa\": \"Benteng Utara\" },{\"ID\": 50,\"kecamatan\": \"Bontomantene\",\"desa\": \"Garaupa\"}]}";
    try {
        JSONObject jsonObject = new JSONObject(result);
        HashMap<String,Integer> hashMap = new HashMap<>();
        for (int i=0;i<jsonObject.getJSONArray("data").length();i++){
            JSONObject innerJsonObject = jsonObject.getJSONArray("data").getJSONObject(i);
            String key = innerJsonObject.getString("kecamatan")+" ["+innerJsonObject.getString("desa")+"]";
            if(hashMap.containsKey(key)){
                int count = hashMap.get(key)+1;
                hashMap.put(key,count);
            }else {
                hashMap.put(key,1);
            }
        }
        Log.e("Hashmap---",hashMap.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.