【问题标题】:String[] from ArrayList<Class>来自 ArrayList<Class> 的字符串 []
【发布时间】:2015-03-09 14:46:07
【问题描述】:

我最近开始开发一个向服务器发出请求并获得 json 响应的应用程序。

“东西”运行良好,直到我不得不在列表中实现新东西,现在我很难修复它。

非常感谢任何帮助:

class RemoteConfig
{

// names and type must match what we get from the remote
String[] username;

ArrayList<accDetails> in_groups;

String[] in_groups_sorted;

class accDetails
{
  int group_id;
  String group_label;
  Boolean _is_system;
}

这只是课程开始的一部分,下面是 json 响应的样子:

{  
"username":[  
    "mike"
],
"in_groups":[  
    {  
        "group_id":2,
        "group_label":"All users",
        "_is_system":true
    },
    {  
        "group_id":4372,
        "group_label":"Privileged User",
        "_is_system":false
    },
    {  
        "group_id":4979,
        "group_label":"Supervisor",
        "_is_system":false
    }
]
}

我现在遇到的问题是,如果 _is_system 值为 false,我不知道如何拆分 in_groups 数组列表并进入 String[] in_groups_sorted Group_label 的值。

非常感谢任何帮助。

谢谢你, 迈克


查看回复后,最干净和最简单的是阿贝提供的:

public String[] groupSettings()
{
    String[] levels = new String[] {};

    if (remoteConfig != null && remoteConfig.in_groups != null){
        for (accDetails ad: remoteConfig.in_groups)
        {
            if (!ad._is_system) {
                levels = ArrayUtils.addAll(levels, ad.group_label); ;
            }
        }
    }

    return levels;
}

【问题讨论】:

  • 你解析json(使用JsonObject/JsonArray等)还是让API为你构建Object?
  • 感谢大家的帮助。我的想法太远了,我忘了保持简单。我使用了@Abbe Resina 提供的部分代码,它工作正常。其他的也成功了,但这是最简单的

标签: java android json arraylist


【解决方案1】:

根据您的问题,我想 JSON 已经被解析并存储在 RemoteConfig 类的 in_groups 字段中。您只需要过滤填充in_group_sorted 字段所需的信息。

将以下内容添加到RemoteConfig 类:

public initGroupSorted() {
   // Temporary list, since we don't know the size once filtered 
   List<String> labels = new ArrayList<>();
   for (accDetails ad : in_groups) {
      if (ad._is_system) {
        groups.add(ad.group_label);
      }
   }
   in_group_sorted = labels.toArray(new String[labels.size()]);
}

【讨论】:

  • 谢谢。我根据自己的需要对其进行了修改,现在它可以工作了:) 简单干净
【解决方案2】:

如果您不想更改解析 JSON 的方式,您可以随时这样做:

让 accDetails 实现 Comparable,然后使用 Collections.sort 传递 in_groups。

如果你真的想要 String[],你总是可以遍历 in_groups,添加到 in_groups_sorted,然后使用 Arrays.sort

【讨论】:

    【解决方案3】:

    迈克,让我给你一些应该让你前进的东西。从您的问题中,我感觉您的问题在于如何解析 JSON,因此在您编写自己的解析器之前,请考虑一下我刚刚编写的以下代码:

    public void createObjects(String rawJSON) {
            try {
                JSONObject object = new JSONObject(rawJSON);
                JSONArray username = object.getJSONArray("username");
                JSONArray inGroups = object.getJSONArray("in_groups");
    
                RemoteConfig config = new RemoteConfig();
                config.in_groups = new ArrayList<>();
                config.username = username.getString(0);
    
                for (int i = 0; i < inGroups.length(); i++) {
                    JSONObject group = inGroups.getJSONObject(i);
                    if (!group.getBoolean("_is_system")) {
                        accDetails details = new accDetails();
                        details.group_id = group.getInt("group_id");
                        details.group_label = group.getString("group_label");
                        details._is_system = false;
                       config.in_groups.add(details);
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
       }
    

    【讨论】:

    • 谢谢。我现在将测试并告诉你它是如何工作的。
    【解决方案4】:

    这是一个使用 Stream's filtersortedmap 方法的 Java 8 解决方案:

    //ArrayList<accDetails> in_groups is already populated
    Stream<accDetails> tempStream= in_groups.stream().filter(p -> p._is_system == false);
    tempStream= tempStream.sorted((accDetails o1, accDetails o2) -> o1.group_label.compareTo(o2.group_label));
    String[] in_groups_sorted = tempStream.map(s -> s.group_label).toArray(String[]::new);
    

    为了可见性而将调用分开,但它们可以是单一的:

    String[] in_groups_sorted = in_groups.stream().filter(p -> p._is_system == false).sorted((accDetails o1, accDetails o2) -> o1.group_label.compareTo(o2.group_label)).map(s -> s.group_label).toArray(String[]::new);
    

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2017-09-03
      • 1970-01-01
      • 2019-08-28
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多