JerryKo
JerryKo

Reputation: 384

Comparing objects with same keys in one json array, Android

I have a json array in this format:

{
  "workHours":[{ 
                 "dayOfWeek":[1,2,3,4,5],
                 "timeInterval":"00:00-00:45"
               },
               {
                 "dayOfWeek":[5,6,0],
                 "timeInterval":"01:00-03:15"
               },
               {
                 "dayOfWeek":[6,0],
                 "timeInterval":"00:00-00:45"
               }]
}

I would like to compare two conditions First I want to check each "dayOfWeek" and check if there are repeated day, for example, the first and second "dayOfWeek" both has a 5, second and third "dayOfWeek" both has 6 and 0. Second I want to check is "timeInterval" is the same, for example the first and third "timeInterval are both "00:00-00:45".

I can now get each object separately but I am not sure how to compare them one by one

for(int i = 0; i<array.length(); i++) {
            JSONObject json = null;
            try{
                json = array.getJSONObject(i);

                JSONArray dayOfWeekArr = json.getJSONArray("dayOfWeek");
                String timeInterval = json.getString("timeInterval");
                } catch (JSONException e) {
                e.printStackTrace();
            }
        } 

Upvotes: 0

Views: 1530

Answers (3)

user7350960
user7350960

Reputation:

Order is a Java class which maintains getter and setter.

public static ArrayList<Order>ParseOrder(String response) throws JSONException 
{
ArrayList<Order> alUser = new ArrayList<>();
 JsonObject jsonroot= new JsonObject("");
JSONArray parentArray = jsonRoot.getJSONArray("workHours");

        for (int j = 0; j < parentArray.length(); j++) {

            JSONObject finalObject = parentArray.getJSONObject(j);

       JsonObject finalobject1=finalobject.getJSONObject("");

           Order user = new Order();
            // Set user values
            user.setdatofweek(finalObject1.getString("dayOfWeek"));
            user.setTimeinterval(finalObject1.getString("timeInterval"));
             // Add user to list
            alUser.add(user);
        }
 // Finally return user list
        return alUser;
    }
}

After Getting Result in responce then you need to store particulat value in variable and then check condition what you need..

Upvotes: 0

ZeroOne
ZeroOne

Reputation: 9117

First(Optional), create model for ease use later

public class Work
{
    private List<WorkHours> workHours;

    public List<WorkHours> getWorkHours ()
    {
        return workHours;
    }
}

public class WorkHours
{
    private String timeInterval;

    private List<Integer> dayOfWeek;

    public String getTimeInterval ()
    {
        return timeInterval;
    }

    public List<Integer> getDayOfWeek ()
    {
        return dayOfWeek;
    }
}

Second, convert json into Model class

Work work = new Gson().fromJson(JSON_STRING,Work.class);
List< WorkHours> wo = work.getWorkHours();

Third, comparing value i think you need to iterate each value and list

for(int x=0;x<wo.size();x++){
    //check workhours
    List<Integer> days1 = wo.get(x).getDayofWeek();
    for(int y=x+1;y<wo.size();y++){
         List<Integer> days2 = wo.get(y).getDayofWeek();
         for(Integer one1:days1){
             //check if array contains same element
             if(days2.contains(one1)){
                 //do your code
             }
         }
    }

    //check time interval
    String timeInterval1 = wo.get(x).getTimeInterval();
    for(int y=x+1;y<wo.size();y++){
        String timeInterval2 = wo.get(y).getTimeInterval();
        //check if same time
        if(timeInterval1.equal(timeInterval2)){
            //do your code
        }
    }
}

Upvotes: 2

Mukesh M
Mukesh M

Reputation: 2290

See above answer: https://stackoverflow.com/a/43729704/5227589 or try this

     String jsonString = "{" +
            "  \"workHours\":[{ " +
            "                 \"dayOfWeek\":[1,2,3,4,5]," +
            "                 \"timeInterval\":\"00:00-00:45\"" +
            "               }," +
            "               {" +
            "                 \"dayOfWeek\":[5,6,0]," +
            "                 \"timeInterval\":\"01:00-03:15\"" +
            "               }," +
            "               {" +
            "                 \"dayOfWeek\":[6,0,4]," +
            "                 \"timeInterval\":\"00:00-00:45\"" +
            "               }]" +
            "}";
    JSONArray arrayOfWorkHours = null;
    try {
        arrayOfWorkHours = new JSONObject(jsonString).getJSONArray("workHours");


        for (int i = 0; i < arrayOfWorkHours.length(); i++) {
            JSONObject jsonObjectofWorkHours = null;

            jsonObjectofWorkHours = arrayOfWorkHours.getJSONObject(i);

            JSONArray dayOfWeekArr = jsonObjectofWorkHours.getJSONArray("dayOfWeek");
            String timeInterval = jsonObjectofWorkHours.getString("timeInterval");

            JSONObject jsonObjectofWorkHoursTemp = null;
            JSONArray dayOfWeekArrTemp;
            String timeIntervalTemp;
            int lastWorkHoursProcessed = -1;

            for (int j = 0; j < dayOfWeekArr.length(); j++) {
                for (int k = i + 1; k < arrayOfWorkHours.length(); k++) {

                    jsonObjectofWorkHoursTemp = arrayOfWorkHours.getJSONObject(k);
                    //Other DayOfWeek Array
                    dayOfWeekArrTemp = jsonObjectofWorkHoursTemp.getJSONArray("dayOfWeek");
                    lastWorkHoursProcessed = k;


                    for (int l = 0; l < dayOfWeekArrTemp.length(); l++) {
                        //Log.i("MyTag",  dayOfWeekArr.get(j).toString());
                        //Log.i("MyTag",   dayOfWeekArrTemp.get(l).toString());


                        if (dayOfWeekArr.get(j).toString().equals(dayOfWeekArrTemp.get(l).toString())) {
                            Log.i("MyTag", "workHours[" + i + "] and workHours[" + k + "]" + " has " + dayOfWeekArrTemp.get(l).toString()
                                    + " as same Value.");
                        }

                    }
                }

            }

            if (lastWorkHoursProcessed != -1) {
                timeIntervalTemp = arrayOfWorkHours.getJSONObject(lastWorkHoursProcessed).getString("timeInterval");
                if (timeInterval.equals(timeIntervalTemp)) {
                    Log.i("MyTag", "workHours[" + i + "] and workHours[" + lastWorkHoursProcessed + "]" + " has same timeInterval");
                }
            }

        }

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

Upvotes: 1

Related Questions