-1

I have two objects

 "Conditions1": [
    {
       "fieldToken": "value1",
       "uniqueName": "value2",
       "conditionOperator": ">",
       "conditionValue": "value3"
     },
     {
       "fieldToken": "value1",
       "uniqueName": "value2",
       "conditionOperator": "==",
       "conditionValue": "value3"
     }
  ]

 "Conditions2": [
     {
       "fieldToken": "value1",
       "uniqueName": "value2",
       "conditionOperator": ">",
       "conditionValue": "value3"
      },
      {
        "fieldToken": "value1",
        "uniqueName": "value2",
        "conditionOperator": "==",
        "conditionValue": "value3"
      }
  ]

I want to compare these two objects whether they are equal or not. Both the objects are same but When I am using equals method it is returning false. How can i compare array of json objects?

Puneet
  • 615
  • 2
  • 7
  • 17
Priyanka Taneja
  • 515
  • 1
  • 5
  • 21

2 Answers2

0

There is such a thing as a JSONObject if you use package org.json. You create a JSONObject:

import org.json.JSONObject;

    JSONObject firstJson = new JSONObject();
    JSONObject secondJson = new JSONObject();
    firstJson.put("test1", "value1");
    secondJson.put("test1", "value1");  // change value1 to something else to test
    boolean result = firstJson.similar(secondJson);
    System.out.println("result: " + result);   

Change value1 or any of the values to test. It will print true or false depending on your test values. The source code says: "They must contain the same set of names which must be associated with similar values." The source can be found here: https://github.com/stleary/JSON-java/blob/master/JSONObject.java

To test an array of JSONObjects, use JSONArray from the same package, and break the array up into individual JSONObjects to compare them.

NSchorr
  • 875
  • 10
  • 13
-2

As you have two objects. take it in two variable and then make it string for both.

e.g.

    var a = Conditions1.toString();
    var b = Conditions2.toString();

    if(Conditions1.toString() === Conditions2.toString()){
        console.log("matched")
     }

Note: above two matched only when both object are same.

Arif Rathod
  • 578
  • 2
  • 13
  • it won't work if the order of json objects within the arrays are different – nafas Mar 16 '18 at 12:32
  • in that case, it will return false, because if the arrays are different then it will not matched and return false. – Arif Rathod Mar 16 '18 at 12:35
  • its all depends on the definition of "equals" : i.e. should we consider the order (e.g. List) or not (e.g Set)? – nafas Mar 16 '18 at 15:14