0
JsonObject json = new JsonObject();
json.addProperty("type", "hello");

System.out.println(json.get("type").toString().equals("hello")); 

The above print statement should technically give "true" right? But am getting "false"...am kind of novice to java..."please bare with this novice question".

Please let me know, whats wrong with the code?

3
  • 1
    try to print this json.get("type").toString() and mystery will be solved Commented Dec 10, 2016 at 14:11
  • Don't use toString, use getAsString. Commented Dec 10, 2016 at 14:15
  • What is the return type of that get? Commented Dec 10, 2016 at 14:16

1 Answer 1

2

Try:

json.get("type").getAsString();

The thing is that GSon JsonObject stores the property inside a Map<String, JsonElement> internally, and when you call a JsonObject.get() method, it returns a JsonElement and not the actual value.

Sign up to request clarification or add additional context in comments.

Comments

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.