X09
X09

Reputation: 3956

How to get random objects from a JSON array

This JSONAray:

"cows": [
    {
      "age": 972,
      "name": "Betty"
      "status": "publish",
      "sticky": "pregnant"
    },
    {
      "age": 977,
      "name"; "Kate"
      "status": "publish",
      "sticky": "heat"
    },
    {
      "age": 959,
      "name": "Julie"
      "site_age": 63178480,
      "sticky": "Nursing"
    },
    ...
 }

that contains 20 objects. What I wanted is this: get 3 random objects out of the 20. And the ages of any of the three won't be a certain number say 961.

Currently this what I am doing:

private void parseCowsReq(JSONObject array) {
         try {
             for (int i = 0; i < 3; i++) {
                 int randumNum = getRandomCow(array);
                 JSONObject jsonObject = array.getJSONObject(randumNum);
                 String cowName = jsonObject.getString("name");
                 String cowStatus = jsonObject.getString("status");
                 Log.d(TAG, "Cow name is " + cowName + "cow Status is " + cowStatus);
             }

         } catch (JSONException e) {
             e.printStackTrace();
         }
  }

  private int getRandomCow(JSONArray jsonArray) {
          int length = jsonArray.length();
          int[] array;
          array = new int[length-1];
          int rnd = new Random().nextInt(array.length);
          return array[rnd];
  }

There are of issues with this code.

  1. I don't know how to ensure that the object gotten in line JSONObject jsonObject = array.getJSONObject(randumNum); won't have an age of 961
  2. The random number gotten is always 0

    Please do you have any idea how this can be done?

Upvotes: 0

Views: 7091

Answers (3)

Amir Panahandeh
Amir Panahandeh

Reputation: 9029

you can do it with this:

public ArrayList<Integer> getRandomObject(JSONArray jsonArray, int indexesWeeNeed){
    Random rn = new Random();
    Set<Integer> generated = new LinkedHashSet<>();
    while(generated.size() < indexesWeeNeed){
        int index = rn.nextInt(10);
        JSONObject jsonObject = (JSONObject) jsonArray.get(index);
        int age = jsonObject.getInt("age");
        if(age<961) {
            generated.add(index);
        }
    }
    ArrayList<Integer> arrayList = new ArrayList<>();
    arrayList.addAll(generated);
    return arrayList;
}

Upvotes: 4

Rogue
Rogue

Reputation: 11483

Well firstly, load the objects:

JSONArray array = /* your array */;

Next, we need a method to retrieve 3 unique objects from the JSONArray (which is actually a List). Let's shuffle the indexes of the json array, so that we don't end up having to repeatedly generate duplicates:

public Stream<JSONObject> randomObjects(JSONArray array, int amount) {
    if (amount > array.size()) {
        //error out, null, return array, whatever you desire
        return array;
    }
    List<Integer> indexes = IntStream.range(0, array.size()).collect(Collectors.toList());
    //random, but less time generating them and keeping track of duplicates
    Collections.shuffle(indexes);
    Set<Integer> back = new HashSet<>();
    Iterator<Integer> itr = indexes.iterator();
    while (back.size() < amount && itr.hasNext()) {
        int val = itr.next();
        if (array.get(val).getInt("age") != 961) { //or any other predicates
            back.add(val);
        }
    }
    return back.stream().map(array::get);
}

Using this, we can select the three objects from the list and utilize them how we wish:

randomObjects(array, 3).map(o -> o.getInt("age")).forEach(System.out::println);
//972
//977
//952

When I said "or any other predicates, you can pass those as well via the method:

public Stream<JSONObject> randomObjects(..., Predicate<Integer> validObject) {
    //...
    int val = itr.next();
    if (validObject.test(val)) {
        back.add(val);
    }
    //...
}

Which would mean you could change the blacklisting per method call:

Stream<JSONObject> random = randomObjects(array, 3, val -> array.get(val).getInt("age") != 961);

Upvotes: 0

JKyleK
JKyleK

Reputation: 31

One part that's messed up is when you call
Random().nextInt(array.length);
array.length is the new array you just created. You need to perform the function on the existing array:
Random().nextInt(jsonArray)
to get a random number other than zero.

As for ensuring you don't get a certain age, I'd suggest breaking up the code to not call the getRandomCow(array) function inside of the for loop. When you retrieve a cow, check the name doesn't match, check the age, and if it works, keep it. If not get another cow.

Upvotes: 0

Related Questions