Gson, jsonelement, string comparison in Java
•
Java
Well, I want to know that it may be very simple and stupid, but after a period of struggle, I don't know what happened
I'm using gson to handle some JSON elements Somewhere in my code, I get a jsonelement of a jsonobject as a string and compare it with another string As far as I know, they are both equal, but I am always false in comparison This is a clip
JsonArray arr; JsonObject jsonobj; JsonElement model_elem; String STUPID_STRING = "bla bla bla"; // PrevIoUsly we initializes and fill arr,it doesn't matter how... I hope jsonobj = arr.get(0).getAsJsonObject(); model_elem = jsonobj.get("coolname"); if (model_elem.toString().equals(STUPID_STRING)) { ...
It never enters an IF statement
Arr has an element at index 0, and jsonobj has a field named "coolname". If I print ln model_ Elem, I get "bla bla bla" (same as stupid_string) I've tried equals() and compareto() = = 0
I can't figure out what's going on here. Who knows- s.
Thank you in advance
Solution
I believe you need to use getasstring () in gson Tostring() will add quotation marks!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码