Lax*_*n G 4 java json gson jsonparser google-json-api
我目前正在使用com.google.gson api(使用gson-2.8.5版本)的JsonObject和JsonParser来解析和读取输入JSON的值。
我有 JSON 归档,例如 smaple,"resultCode":"SUCCESS",当我尝试从 json 读取相同的值时,它给出的结果为""SUCCESS"".
我正在阅读的每个值都带有双“”,不知道为什么?您可以参考我的调试屏幕的下面屏幕。
我是 Json 和解析器的新手,这是默认行为吗?
我期待"SUCCESS","S"不像
我
在下图中突出显示的"00000000"那样""SUCCESS""。""S""""00000000""
请分享我们如何获得字符串的绝对值而不使用“””双引号字符串的任何想法,它会导致我的字符串比较失败。
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));
}
Run Code Online (Sandbox Code Playgroud)
这是我的输入 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=="
}
Run Code Online (Sandbox Code Playgroud)
JsonParser将您的 json 解析为JsonElement结构。您看到的行为是正常的,因为您正在使用toString的方法JsonElement。要实现您的目标,只需使用JsonElement::getAsString方法:
String resultCode = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCode").getAsString();
Run Code Online (Sandbox Code Playgroud)
这给出了SUCCESS而不是"SUCCESS"
请注意,这JsonElement是一个抽象类,扩展该类的类将覆盖这些辅助getAs...方法。在你的情况下JsonPrimitive::getAsString将被调用。
您还可以为 json 创建一个 POJO 类,并用于Gson::fromJson将 json 解析为 POJO 类的对象。