Reputation: 1163
I'm using Jettison to make JSONObjects out of some Strings
I want to get a list of all the keys in one JSONObject (j1) and compare the values to the values attached to the keys in the other JSONObject (j2).
I'm trying to create something where I don't care about the type of the value since the values are character strings and ints and who knows what else.
So I'm doing something like:
I want to do something like
Object o1 = j1.get("key")
Object o2 = j2.get("key")
What can I convert o1 & o2 into so I can compare their values? JSONObject? What if the values are JSONArrays?
I have tried JSONObject and I get a
"ClassCastException org.codehaus.jettison.json.JSONObject$Null cannot be cast to org.codehaus.jettison.json."
when I have:
{"key1":null, .....
What options do I have with that?
Upvotes: 0
Views: 242
Reputation: 140299
Assuming you are comparing the values for equality (and not, say, with respect to some ordering), you can just use Object.equals
:
o1.equals(o2)
since the signature, even in classes extending Object
, is
public boolean equals(Object other)
so you can compare any object to any other, no conversion or explicit casting needed (implementations of equals
will likely do casting internally, though).
If one or both of them can be null, you can use java.util.Objects.equals
to avoid the potential NPE:
Objects.equals(o1, o2)
Upvotes: 0