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

时间:2017-05-02 02:47:21

标签: 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"
               },
               {
                 "dayOfWeek":[6,0],
                 "timeInterval":"00:00-00:45"
               }]
}

我想比较两个条件   首先,我想检查每一天" dayOfWeek"并检查是否有重复的一天,例如,第一天和第二天" dayOfWeek"两者都有5年,第2年和第3天,以及#34; 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();
            }
        } 

3 个答案:

答案 0 :(得分:2)

首先(可选),创建模型以便日后使用

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

其次,将json转换为Model类

Work work = new Gson().fromJson(JSON_STRING,Work.class);
List< WorkHours> wo = 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
        }
    }
}

答案 1 :(得分:1)

见上面的回答:https://stackoverflow.com/a/43729704/5227589或试试这个

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

答案 2 :(得分:0)

Order是一个维护getter和setter的Java类。

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

在获得结果后,您需要将特定值存储在变量中,然后检查条件您需要什么..