如何从android中的JSONArray检查两个值是否相同?

时间:2014-05-31 05:32:17

标签: android json arraylist hashmap

我得到的Json回复如下:

{
messages: [
{
id: "83",
payer_id: "5",
payee_id: "24",
payer_name: "vishal ",
payee_name: "ravi ",
payer_image: "upload/123.jpg",
payee_image: "upload/crop_110x110_1338731270_591178.jpg",
dispute_id: "43",
subject: "",
message_body: "dwedwde",
added_on: "2014-01-16 04:06:35",
sender_id: "5",
read: "1"
},
{
id: "716",
payer_id: "5",
payee_id: "6",
payer_name: "vishal ",
payee_name: "vishal  ",
payer_image: "upload/123.jpg",
payee_image: "upload/yt.png",
dispute_id: "43",
subject: "",
message_body: "smthig",
added_on: "2014-05-27 02:26:04",
sender_id: "6",
read: "1"
}
],
status: "success"
}

我已经编码如下解析。

ArrayList<HashMap<String, String>> msgList;
msgList = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(disputeURL, BackendAPIService.GET);

            Log.print("Response: ", "> " + jsonStr);

            try {
                if (jsonStr != null) {
                    jsonObj = new JSONObject(jsonStr);

                    if (jsonObj.has(Const.TAG_MESSAGES)) {
                        System.out.println(":::::::::::::has::::::::::::");
                        msgArray = jsonObj.getJSONArray(Const.TAG_MESSAGES);
                        if (msgArray != null && msgArray.length() != 0) {
                            // looping through All Contacts
                            for (int i = 0; i < msgArray.length(); i++) {
                                System.out.println("::::::::::::ARRAY:::::::::::");
                                JSONObject c = msgArray.getJSONObject(i);
                                id = c.getString("id");
                                payee_id = c.getString("payee_id");
                                payer_id = c.getString("payer_id");
                                payer_name = c.getString("payer_name");
                                payee_name = c.getString("payee_name");
                                payer_image = c.getString("payer_image");
                                payee_image = c.getString("payee_image");
                                dispute_id = c.getString("dispute_id");
                                subject = c.getString("subject");
                                message_body = c.getString("message_body");
                                added_on = c.getString("added_on");
                                sender_id = c.getString("sender_id");
                                read = c.getString("read");
                                dispute_id = c.getString(Const.TAG_DIPUTE_ID);

                                System.out.println("::::::::::::sender Id::::::::::" + sender_id + ":::::::::::::::");
                                System.out.println("::::::::::::payee ID::::::::::" + payee_id + ":::::::::::::::");
                                HashMap<String, String> disputeMsgMap = new HashMap<String, String>();

                                disputeMsgMap.put(Const.TAG_ID, id);
                                disputeMsgMap.put(Const.TAG_PAYEE_ID, payee_id);
                                disputeMsgMap.put(Const.TAG_PAYER_ID, payer_id);
                                disputeMsgMap.put(Const.TAG_PAYER_NAME, payer_name);
                                disputeMsgMap.put(Const.TAG_PAYEE_NAME, payee_name);
                                disputeMsgMap.put(Const.TAG_PAYER_IMAGE, payer_image);
                                disputeMsgMap.put(Const.TAG_PAYEE_IMAGE, payee_image);
                                disputeMsgMap.put(Const.TAG_DIPUTE_ID, dispute_id);
                                disputeMsgMap.put(Const.TAG_SUBJECT, subject);
                                disputeMsgMap.put(Const.TAG_MESSAGE_BODY, message_body);
                                disputeMsgMap.put(Const.TAG_ADDED_ON, added_on);
                                disputeMsgMap.put(Const.TAG_READ, read);
                                disputeMsgMap.put(Const.TAG_SENDER_ID, sender_id);
                                msgList.add(disputeMsgMap);

                            }
                        }

所以,我想检查JSONObjects的“sender_Id”是否不同;我只是想查看任何对象的天气sender_id是不同的还是全部相同。

1 个答案:

答案 0 :(得分:4)

您可以在将HashMap添加到列表之前迭代到所有sender_id列表,然后检查当前hashmap's是否与sender_id {{1}的最后一个列表重复}}

示例:

//TOP CODE ARE YOURS
if(msgList.size() != 0)
{
  for(HashMap<String, String> hash :  msgList) {
     if(hash.get(sender_id) != null)
       //YOU have a duplication of id
  }
}
msgList.add(disputeMsgMap);