0

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

3 Answers 3

2

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

4 Comments

Thanks for replying, I am trying out your code but have some problems, what does your "wo" stand for, I tried using work but then get(y) method cannot be resolved, I tried using an array but then getDayOfWeek cannot be resolved
oh..sorry.. it come from Work wo = new Gson().fromJson(JSON_STRING,Work.class);
Ok, but when I use that wo.get(x) the get(x) method cannot be resolved, also is the JSON_STRING my result string that I have provided in my question?
json string is your json.. i fix the wo variable.. my mistake
1

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

Comments

0

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..

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.