0

I recently started working on an app that does a request to a server and gets a json response.

The "thing" functioned beautifully until i had to implement new stuff in the list and now i have a hard time to fix it.

Any help is very appreciated:

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

This is just a part of how the class starts, and here is how the json reponse looks like:

{  
"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
    }
]
}

The problem that i encounter now, is that i have no idea on how to split the in_groups array list and get into String[] in_groups_sorted the value of Group_label if the _is_system value is false.

Any help is highly appreciated.

Thank you, Mike


After checking the responses, the cleanest and simplest was the one provided by Abbe:

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;
}
2
  • You parse the json (using JsonObject/JsonArray and so on) or you let the API build the Object for you? Commented Mar 9, 2015 at 15:03
  • Thank you everyone for your help. I was going to far with my thinking and i forgot to keep it simple. I used part of the code that @Abbe Resina offered and it worked properly. The others were successful as well, but this was the simplest Commented Mar 9, 2015 at 16:11

4 Answers 4

1

From your question, I suppose the JSON is already parsed and stored in the in_groups field of RemoteConfig class. And you just need to filter the information you need to populate the in_group_sorted field.

Add the following to the RemoteConfig class:

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()]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I modified it for my needs and now it works :) Simple and clean
1

if you don´t want to change the way you parse your JSON, you could always do this:

Let accDetails implement Comparable and then use Collections.sort passing in_groups.

if you really want the String[] you could always iterate over in_groups, add to in_groups_sorted and then using Arrays.sort

Comments

0

Mike, let me give you something that should get you going. From your question i got the feeling that your problem was on how to parse the JSON, so before you go write your own parser, consider the following piece of code that i just wrote:

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

1 Comment

Thank you. I wil test now and let you know how it works.
0

Here is a Java 8 Solution using Stream's filter,sorted, and map methods:

//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);

Separated the calls for visibility, but they can be a one liner:

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

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.