有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ArrayList<Class>java字符串[]

我最近开始开发一个应用程序,它向服务器发出请求并获得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_系统值为false,我不知道如何拆分in_groups数组列表并进入字符串[]in_groups_排序组标签的值

非常感谢您的帮助

谢谢,, 迈克


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

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;
}

共 (4) 个答案

  1. # 1 楼答案

    下面是一个使用Stream'sfiltersortedmap方法的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);
    
  2. # 2 楼答案

    如果你不想改变解析JSON的方式,你可以这样做:

    让accDetails实现Comparable,然后使用Collections。按组对传递进行排序

    如果你真的想要字符串[],你可以在组中迭代,添加到组中,然后使用数组排序。排序

  3. # 3 楼答案

    根据你的问题,我假设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()]);
    }
    
  4. # 4 楼答案

    迈克,让我给你点东西让你走吧。从你的问题中,我感觉到你的问题在于如何解析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();
            }
       }