5

I'm using GSON to validate a string which is in JSON format:

String json="{'hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}";
Gson gson=new Gson();
Response resp = new Response();
RequestParameters para = null;
try{
    para = gson.fromJson(json, RequestParameters.class);
}catch(Exception e){
    System.out.println("invalid json format");
}

Its doing good but when I remove the quotes like below, I have removed from hashkey:

"{hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}"

It's still validating it as a proper JSONformat and not throwing any exception and not going in catch body. Any reason why it is doing so? How would I solve this?

RequestParameters class:

public class RequestParameters {
    HashKey hashkey;
    String operation;
    int count;
    int otid;
    String[] attributes;

}
2
  • Please post your RequestParameters class as well so we can see what fields it is mapping to. Commented Jul 31, 2014 at 15:05
  • See also How to check if JSON is valid in Java using GSON on working gson strict parsing workaround. Commented Jul 5, 2019 at 10:25

1 Answer 1

3

Now it will treat second quote as part of hashkey. Have a look at below json string that is get back from the object.

I tested it at jsonlint

{
  "hashkey\u0027": {
    "calculatedHMAC": "f7b5addd27a221b216068cddb9abf1f06b3c0e1c",
    "secretkey": "12345"
  },
  "operation\u0027": "read",
  "attributes": [
    "name",
    "id"
  ],
  "otid": "12"
}

sample code:

String json = "{hashkey':{'calculatedHMAC':'f7b5addd27a221b216068cddb9abf1f06b3c0e1c','secretkey':'12345'},operation':'read','attributes':['name','id'],'otid':'12'}";
Gson gson = new Gson();
try {
    Object o = gson.fromJson(json, Object.class);
    System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(o));
} catch (Exception e) {
    System.out.println("invalid json format");
}

Is there quote needed around JSON string against keys?

Read more...

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

2 Comments

Actually in my case i am not storing it in a string but i am using rest client to send uri to rest service and i send body as following: {"hashkey":{"calculatedHMAC":"f7b5addd27a221b216068cddb9abf1f06b3c0e1c","secretkey":"12345"},"operation":"read","attributes":["name","id"],"otid":"12"} It is then stored in a string in rest service as rest service is receiving it as a String but when i print it. It is printed same: {"hashkey":{"calculatedHMAC":"f7b5addd27a221b216068cddb9abf1f06b3c0e1c","secretkey":"12345"},"operation":"read","attributes":["name","id"],"otid":"12"} So here no single quotes.
same thing applies for double quotes as well.

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.