我目前正在使用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));
}
这是我的输入 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=="
}
最佳答案
JsonParser
将您的 json 解析为 JsonElement
结构。您看到的行为是正常的,因为您使用的是 JsonElement
的 toString
方法。要实现您的目标,只需使用 JsonElement::getAsString
方法:
String resultCode = jo.get("response").getAsJsonObject().get("body").getAsJsonObject().get("resultInfo").getAsJsonObject().get("resultCode").getAsString();
给出SUCCESS
而不是“SUCCESS”
请注意,JsonElement
是一个抽象类,扩展该类的类将覆盖那些辅助 getAs...
方法。在你的情况下JsonPrimitive::getAsString
将被调用。
您还可以为您的 json 创建一个 POJO 类并使用 Gson::fromJson
将 json 解析为 POJO 类的对象。
关于java - 为什么 JsonParser 使用 com.google.gson API 在返回值中给出双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57933747/