繁体   English   中英

为什么JsonParser使用com.google.gson API在返回值中使用双引号引起来

[英]Why JsonParser gives double quotes in the return value, using com.google.gson 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));
    }

这是我输入的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您看到的行为是正常的。 要实现您的目标,只需使用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类的对象。

使用@Michalk的输入:我了解读取JSON数据的简单方法是使用Gson :: fromJson并为json创建POJO类。

我已经使用此链接生成了提供我的示例输入JSON的POJO类,现在我有了名为:CreateOrderJSONResponse的POJO类。

Gson::fromJson

样品:

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

Accessubg数据:

    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();

上面的Gson::fromJson示例运行流畅,与使用以下示例代码直接访问文件相比,它看上去很整洁:

 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();

注意:我已经找到了JSON或JAVA,SCALA,POJO生成器工具的此链接 ,因为GitHub访问可以在此处访问

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM