4

I am currently using JsonObject and JsonParser of com.google.gson api (using gson-2.8.5 version) to parse and read the value form input JSON.

I have JSON filed like , smaple "resultCode":"SUCCESS", when I try to read the same value from json it gives the result as ""SUCCESS"" .

Every value I am reading, getting with double "" not sure why ? You can refer below screen of my debugging screen.

I am new to Json and parser, is that default behavior ?

I am expecting "SUCCESS", "S", "00000000" not like ""SUCCESS"" or ""S"" or ""00000000"" same I have highlighted in the below image .

Please share any idea how we can get apbsolute vlaue of string without """" double quote string it causing my string comparison fail.

enter image description here

String response_result = "{\"response\": {\"head\": {\"function\": \"acquiring.order.create\",\"version\": \"2.0\",\"clientId\": \"201810300000\",\"reqMsgId\": \"56805892035\",\"respTime\": \"2019-09-13T13:18:08+08:00\"},\"body\": {\"resultInfo\": {\"resultCode\": \"SUCCESS\",\"resultCodeId\": \"00000000\",\"resultStatus\": S,\"resultMsg\": \"SUCCESS\"},\"acquirementId\": \"2018080834569894848930\",\"merchantTransId\": \"5683668701112717398\",\"checkoutUrl\": \"http://localhost:8081/crm/operator/operator-search-init.action\"}},\"signature\":\"d+TUYLvt1a491R1e6aO8i9VwXWzVhfNgnhD0Du74f4RgBQ==\"}";
        HttpInvoker.Result result = i.new Result(200, response_result);
    JsonObject jo =  new JsonParser().parse(response_result).getAsJsonObject();

    String resultCode = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCode").toString();
    String resultCodeId = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCodeId").toString();
    String resultStatus = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultStatus").toString();
    String checkoutUrl = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("checkoutUrl").toString();

    if ( RESULT_CODE_GCASH_SUCCESS.equals(resultCode)
            && RESULT_STATUS_SUCCESS.equals(resultStatus)
            && StringUtils.isNotEmpty(checkoutUrl)) {

        log.error("Testing ".concat(resultCode).concat(resultStatus).concat(checkoutUrl));
    }
    log.error("Testing ".concat(resultCode).concat(resultStatus).concat(checkoutUrl));
    }

This is my input JSON

{ 
   "response":{ 
      "head":{ 
         "function":"acquiring.order.create",
         "version":"2.0",
         "clientId":"201810300000",
         "reqMsgId":"56805892035",
         "respTime":"2019-09-13T13:18:08+08:00"
      },
      "body":{ 
         "resultInfo":{ 
            "resultCode":"SUCCESS",
            "resultCodeId":"00000000",
            "resultStatus":"S",
            "resultMsg":"SUCCESS"
         },
         "acquirementId":"2018080834569894848930",
         "merchantTransId":"5683668701112717398",
         "checkoutUrl":"http://localhost:8081/crm/operator/operator-search-init.action"
      }
   },
   "signature":"d+TUYLvtI38YL2hresd98Ixu1BXccvvh1IQMiHuMXUEeW/N5exUsW491R1e6aO8i9VwXWzVhfNgnhD0Du74f4RgBQ=="
}
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
Gautam
  • 3,707
  • 5
  • 36
  • 57

2 Answers2

5

JsonParser parses your json into JsonElement structure. The behaviour that you see is a normal since you are using toString method of JsonElement. To achieve your goal just use JsonElement::getAsString method :

String resultCode = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCode").getAsString();

which gives SUCCESS instead of "SUCCESS"

Note that JsonElement is an abstract class and classes, that extend this class, will override those helper getAs... methods. In your case JsonPrimitive::getAsString will be invoked.

Also you could create a POJO class for your json and use Gson::fromJson to parse json into object of your POJO class.

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
  • but when i see API JsonElement::getAsString for public String getAsString() { throw new UnsupportedOperationException(this.getClass().getSimpleName()); } – Gautam Sep 14 '19 at 08:35
  • 1
    `JsonElement` is an abstract class and this method is overriden by subclasses. In your case it will be `JsonPrimitive::getAsString` that will be invoked. – Michał Krzywański Sep 14 '19 at 08:36
  • Yes now i am getting value without """ I am new to JSON is this correct way to read the value : -------------------------------------------- jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().getAsJsonPrimitive("resultCode").getAsString(); from above code snippet – Gautam Sep 14 '19 at 08:43
  • 1
    Yes it is correct. Both ways will work - yours and mine in the answer. I would also encourage you to look at `Gson::fromJson` and creating POJO class for your json. Then it would be easier for you to access the data from object of your POJO class. – Michał Krzywański Sep 14 '19 at 08:46
  • I understood, your point Gson :: fromJson it really smooth and neat way to access data in the JSAON. I will post the answer here. – Gautam Sep 15 '19 at 04:56
  • Bloody hell, this has been driving me crazy for the last 2 days. ... thanks – interstar Jul 18 '21 at 17:27
2

With the input from @Michalk: I understand that easy way to read JSON data is using Gson::fromJson and creating POJO class for out json.

I have generated POJO Classes supplying my sample input JSON using this link and Now I have POJO Classes called : CreateOrderJSONResponse

Gson::fromJson

Sample :

Gson gson = new Gson();
CreateOrderJSONResponse responseJson = gson.fromJson(inputJSON, CreateOrderJSONResponse.class);

Accessubg data :

    String resultCodeText =   responseJson.getResponse().getBody().getResultInfo().getResultCode();
    String resultCodeId =     responseJson.getResponse().getBody().getResultInfo().getResultCodeId();
    String resultStatus =     responseJson.getResponse().getBody().getResultInfo().getResultStatus();
    String checkoutUrl =      responseJson.getResponse().getBody().getCheckoutUrl();

Above Gson::fromJson example works smooth and it looks neat compare to direct accessing the filed with below sample code :

 JsonObject jo = parser.parse(inputJSON).getAsJsonObject();

 String resultCodeText = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().getAsJsonPrimitive("resultCode").getAsString();
 String resultCodeId = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().getAsJsonPrimitive("resultCodeId").getAsString();
 String resultStatus = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().getAsJsonPrimitive("resultStatus").getAsString();
 String checkoutUrl = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().getAsJsonPrimitive("checkoutUrl").getAsString();

Note : I have found this link of JSON or JAVA, SCALA, POJO generator tools as GitHub access you can access here

Gautam
  • 3,707
  • 5
  • 36
  • 57