在一个json数组中比较具有相同键的对象,Android

在一个json数组中比较具有相同键的对象,Android,android,arrays,json,Android,Arrays,Json,我有一个json数组,格式如下: { "workHours":[{ "dayOfWeek":[1,2,3,4,5], "timeInterval":"00:00-00:45" }, { "dayOfWeek":[5,6,0], "timeInterval":"01:00-03:15"

我有一个json数组,格式如下:

{
  "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"
               }]
}
我想比较两种情况 首先,我想检查每个“dayOfWeek”,并检查是否有重复的日期,例如,第一个和第二个“dayOfWeek”都有一个5,第二个和第三个“dayOfWeek”都有6和0。 第二个我想检查的是“timeInterval”是否相同,例如第一个和第三个“timeInterval”都是“00:00-00:45”

我现在可以分别得到每个对象,但我不知道如何逐一比较它们

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();
            }
        } 
对于(inti=0;i首先(可选),创建模型以便于以后使用

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;
    }
}
公共课堂作业
{
私人列表工作时间;
公共列表getWorkHours()
{
返回工作时间;
}
}
公共课工作时间
{
私有字符串时间间隔;
星期一私人名单;
公共字符串getTimeInterval()
{
返回时间间隔;
}
公共列表getDayOfWeek()
{
星期五返回日;
}
}
其次,将json转换为模型类

Work work = new Gson().fromJson(JSON_STRING,Work.class);
List< WorkHours> wo = work.getWorkHours();
Work-Work=new Gson().fromJson(JSON_字符串,Work.class);
Listwo=work.getWorkHours();
第三,比较价值 我认为您需要迭代每个值和列表

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
        }
    }
}
对于(int x=0;x首先(可选),创建模型以便于以后使用

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;
    }
}
公共课堂作业
{
私人列表工作时间;
公共列表getWorkHours()
{
返回工作时间;
}
}
公共课工作时间
{
私有字符串时间间隔;
星期一私人名单;
公共字符串getTimeInterval()
{
返回时间间隔;
}
公共列表getDayOfWeek()
{
星期五返回日;
}
}
其次,将json转换为模型类

Work work = new Gson().fromJson(JSON_STRING,Work.class);
List< WorkHours> wo = work.getWorkHours();
Work-Work=new Gson().fromJson(JSON_字符串,Work.class);
Listwo=work.getWorkHours();
第三,比较价值 我认为您需要迭代每个值和列表

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
        }
    }
}
for(intx=0;x参见上面的答案:或者试试这个

     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();
    }
String jsonString=“{”+
“\“工作时间\”:[{”+
“\“dayOfWeek\”:[1,2,3,4,5],”+
“时间间隔”:“00:00-00:45”+
"               }," +
"               {" +
“\“dayOfWeek\”:[5,6,0],”+
“时间间隔”:“01:00-03:15”+
"               }," +
"               {" +
“\“dayOfWeek\”:[6,0,4],”+
“时间间隔”:“00:00-00:45”+
"               }]" +
"}";
JSONArray arrayOfWorkHours=null;
试一试{
arrayOfWorkHours=新的JSONObject(jsonString).getJSONArray(“工时”);
对于(int i=0;i
参见上面的答案:或者试试这个

     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();
    }
String jsonString=“{”+
“\“工作时间\”:[{”+
“\“dayOfWeek\”:[1,2,3,4,5],”+
“时间间隔”:“00:00-00:45”+
"               }," +
"               {" +
“\“dayOfWeek\”:[5,6,0],”+
“时间间隔”:“01:00-03:15”+
"               }," +
"               {" +
“\“dayOfWeek\”:[6,0,4],”+
“时间间隔”:“00:00-00:45”+
"               }]" +
"}";
JSONArray arrayOfWorkHours=null;
试一试{
arrayOfWorkHours=新的JSONObject(jsonString).getJSONArray(“工时”);
对于(int i=0;i