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;

}
abhi
  • 1,760
  • 1
  • 24
  • 40
Mr37037
  • 738
  • 2
  • 13
  • 29
  • Please post your RequestParameters class as well so we can see what fields it is mapping to. – Willie Nel Jul 31 '14 at 15:05
  • See also [How to check if JSON is valid in Java using GSON](https://stackoverflow.com/questions/43233898/how-to-check-if-json-is-valid-in-java-using-gson/47890960#47890960) on working gson strict parsing workaround. – Vadzim Jul 05 '19 at 10:25

1 Answers1

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...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 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. – Mr37037 Jul 31 '14 at 15:23
  • same thing applies for double quotes as well. – Braj Aug 01 '14 at 05:21