0

I am getting a Json response as below:

{
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"
}

I have coded as below for parsing.

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

                            }
                        }

So, I want to check that "sender_Id" of JSONObjects are different or not; I just want to check weather sender_id of any object is different or all are same.

1 Answer 1

4

You can iterate to all of your list of HashMap before you add it to the list and then check if the current sender_id is duplicated from the last list of hashmap's sender_id

example:

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

6 Comments

Thank you for help,But friend can you tell me how can i compare arrayList's element? means if all the elements are same or not?because I have "msgList" is an arrayList,SO I will compare for "sender_id"..Please tell me ..thank you
yes..."sender_id" of any object is different or all the "sender_id" are same.
@user3686229 then use the get method the way that i just did and compare all the field on the json object
if(hash.get(sender_id) != null) tells what?
@user3686229 if the current id is already in the list if hashMap
|

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.