将JSON中的数据分组并显示在列表中

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

Grouping data from JSON and display on a list

问题

我有一个类似这样的JSON数据:

{
  "items": [
    {
      "kodeHp": "C-1",
      "firstName": "iman",
      "lastName": "firmansyah",
      "email": "ggag@gmail.com"
    },
    {
      "kodeHp": "C-2",
      "firstName": "dini",
      "lastName": "yyyy",
      "email": "ggag@gmail.com"
    },
    {
      "kodeHp": "C-1",
      "firstName": "tanu",
      "lastName": "xxxxx",
      "email": "bb@gmail.com"
    }
  ]
}

我想在列表上显示“firstName”,但按“kodeHp”对象排序。所以我的列表将如下所示:

{
  "iman": "C1",
  "tanu": "C1",
  "dini": "C2"
}

这是我尝试过的:

private String parseItems(String jsonResponse) {
    ArrayList<HashMap<String,String>> list = new ArrayList<>();

    try {
        JSONObject jObj = new JSONObject(jsonResponse);
        JSONArray jArray = jObj.getJSONArray("items");

        for(int i = 0; i < jArray.length(); i++){
            JSONObject jo = jArray.getJSONObject(i);
            String kodeHp = jo.getString("kodeHp");
            String firstName = jo.getString("firstName");
            String lastName = jo.getString("lastName");
            String email = jo.getString("email");

            HashMap<String, String> item = new HashMap<>();
            if (!item.containsKey(kodeHp)){
                item.get(kodeHp);
            }
            item.put("firstName", firstName);
            item.put("lastName", lastName);
            item.put("email",email);
            item.put("kodeHp", kodeHp);
            list.add(item);
        }
    } catch (JSONException e){

    }
    adapter = new SimpleAdapter(this, list, R.layout.item_list_row,
            new String[]{"firstName", "kodeHp"}, new int[] {R.id.TvFirstName,R.id.TvKodeHp});

    listView.setAdapter(adapter);
    loading.dismiss();
    return jsonResponse;
}

它可以显示我的JSON数据,但尚未按我想要的方式排序。有什么建议吗?

英文:

I have a JSON data like this:

{
&quot;items&quot;: [
{
&quot;kodeHp&quot;: &quot;C-1&quot;,
&quot;firstName&quot;: &quot;iman&quot;,
&quot;lastName&quot;: &quot;firmansyah&quot;,
&quot;email&quot;: &quot;ggag@gmail.com&quot;
},
{
&quot;kodeHp&quot;: &quot;C-2&quot;,
&quot;firstName&quot;: &quot;dini&quot;,
&quot;lastName&quot;: &quot;yyyy&quot;,
&quot;email&quot;: &quot;ggag@gmail.com&quot;
},
{
&quot;kodeHp&quot;: &quot;C-1&quot;,
&quot;firstName&quot;: &quot;tanu&quot;,
&quot;lastName&quot;: &quot;xxxxx&quot;,
&quot;email&quot;: &quot;bb@gmail.com&quot;
},

I want to display the "firstName" on a list but sorted by the "kodeHp" object. So my list gonna looks like this:

{
iman: C1
tanu: C1
dini: C2
}

This is what I have tried:

private String parseItems(String jsonResponse) {
ArrayList&lt;HashMap&lt;String,String&gt;&gt; list = new ArrayList&lt;&gt;();
try {
JSONObject jObj = new JSONObject(jsonResponse);
JSONArray jArray = jObj.getJSONArray(&quot;items&quot;); //edited on gscript
for(int i = 0; i &lt; jArray.length(); i++){
JSONObject jo = jArray.getJSONObject(i);
String kodeHp = jo.getString(&quot;kodeHp&quot;);
String firstName = jo.getString(&quot;firstName&quot;);
String lastName = jo.getString(&quot;lastName&quot;);
String email = jo.getString(&quot;email&quot;);
HashMap&lt;String, String&gt; item = new HashMap&lt;&gt;();
if (!item.containsKey(kodeHp)){
item.get(kodeHp);
}
item.put(&quot;firstName&quot;, firstName);
item.put(&quot;lastName&quot;, lastName);
item.put(&quot;email&quot;,email);
item.put(&quot;kodeHp&quot;, kodeHp);
list.add(item);
}
} catch (JSONException e){
}
adapter = new SimpleAdapter(this, list, R.layout.item_list_row,
new String[]{&quot;firstName&quot;, &quot;kodeHp&quot;}, new int[] {R.id.TvFirstName,R.id.TvKodeHp});
listView.setAdapter(adapter);
loading.dismiss();
return jsonResponse;
}

It works to display my JSON data but not yet sorted like I want. Any suggestion?

答案1

得分: 2

要对列表进行排序,您可以使用 List.sort 方法,配合适当的 Comparator,例如使用 Comparator.comparing

list.sort(Comparator.comparing(map -> map.get("kodeHp")));

请注意,这将按照 &quot;kodeHp&quot; 的值按字母顺序进行排序。还请注意,如果您的输入数据中可能存在 null 值,则必须添加处理方式。


如果您需要一个 Java 8 之前的解决方案,例如用于旧版本的 Android,您需要创建一个老式的匿名类,例如:

list.sort(new Comparator<Map<String,String>>() {
    @Override
    public int compare(Map<String,String> o1, Map<String,String> o2) {
        return o1.get("kodeHp").compareTo(o2.get("kodeHp"));
    }
});
英文:

To sort your list, you can use List.sort with an appropriate Comparator, for example using Comparator.comparing:

list.sort(Comparator.comparing(map -&gt; map.get(&quot;kodeHp&quot;)));

Note that this will sort alphabetically by the values of &quot;kodeHp&quot;. Also note that you will have to add handling for null values if they are possible in you input data.


In case you need a pre-Java8 solution, e.g. for old Android versions, you'll have to create an old-fashioned anonymous class instead, for example:

list.sort(new Comparator&lt;Map&lt;String,String&gt;&gt;() {
@Override
public int compare(Map&lt;String,String&gt; o1, Map&lt;String,String&gt; o2) {
return o1.get(&quot;kodeHp&quot;).compareTo(o2.get(&quot;kodeHp&quot;));
}
});

huangapple
  • 本文由 发表于 2020年9月21日 17:31:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63989598.html
匿名

发表评论

匿名网友

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

确定