简体   繁体   English

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

[英]Why JsonParser gives double quotes in the return value, using com.google.gson API

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. 我目前正在使用com.google.gson api(使用gson-2.8.5版本)的JsonObject和JsonParser来解析和读取输入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"" . 我有JSON文件,例如,smaple "resultCode":"SUCCESS",当我尝试从json读取相同的值时,结果为""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 ? 我是Json和解析器的新手,这是默认行为吗?

I am expecting "SUCCESS" , "S" , "00000000" not like ""SUCCESS"" or ""S"" or ""00000000"" same I have highlighted in the below image . 我期望"SUCCESS""S""00000000"不像我在下图中突出显示的""SUCCESS""""S""""00000000""一样。

Please share any idea how we can get apbsolute vlaue of string without """" double quote string it causing my string comparison fail. 请分享任何想法,我们如何获得不含“””双引号字符串的字符串的绝对值,这会导致我的字符串比较失败。

在此处输入图片说明

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 这是我输入的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 parses your json into JsonElement structure. JsonParser将您的json解析为JsonElement结构。 The behaviour that you see is a normal since you are using toString method of JsonElement . 由于使用JsonElement toString方法, JsonElement您看到的行为是正常的。 To achieve your goal just use JsonElement::getAsString method : 要实现您的目标,只需使用JsonElement::getAsString方法:

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

which gives SUCCESS instead of "SUCCESS" 它给出了SUCCESS而不是"SUCCESS"

Note that JsonElement is an abstract class and classes, that extend this class, will override those helper getAs... methods. 请注意, JsonElement是一个抽象类,扩展该类的类将覆盖那些帮助程序的getAs...方法。 In your case JsonPrimitive::getAsString will be invoked. 在您的情况下,将调用JsonPrimitive::getAsString

Also you could create a POJO class for your json and use Gson::fromJson to parse json into object of your POJO class. 也可以为json创建一个POJO类,并使用Gson::fromJson将json解析为POJO类的对象。

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. 使用@Michalk的输入:我了解读取JSON数据的简单方法是使用Gson :: fromJson并为json创建POJO类。

I have generated POJO Classes supplying my sample input JSON using this link and Now I have POJO Classes called : CreateOrderJSONResponse 我已经使用此链接生成了提供我的示例输入JSON的POJO类,现在我有了名为:CreateOrderJSONResponse的POJO类。

Gson::fromJson

Sample : 样品:

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

Accessubg data : 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();

Above Gson::fromJson example works smooth and it looks neat compare to direct accessing the filed with below sample code : 上面的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();

Note : I have found this link of JSON or JAVA, SCALA, POJO generator tools as GitHub access you can access here 注意:我已经找到了JSON或JAVA,SCALA,POJO生成器工具的此链接 ,因为GitHub访问可以在此处访问

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

相关问题 使用com.google.gson进行数据输入 - Data entry using com.google.gson 如何使用com.google.gson类创建json对象 - How to create json object using com.google.gson class 如何在JsonArray(com.google.gson)中将Empty字符串转换为null - How to convert Empty string to null in JsonArray ( com.google.gson) 如何修复 Android Studio 中的“无法解析:com.google.gson:gson:2.8.5” - How to fix "Failed to resolve: com.google.gson:gson:2.8.5" in Android studio 无法使用 gson.toJson(Element) 获取 JSON 字符串,{模块 java.base 不会“打开 java.util”到模块 com.google.gson] - Can't get JSON String by using gson.toJson(Element), {module java.base does not "opens java.util" to module com.google.gson] java maven 导入问题:错误:com.google.gson 包不存在 - java maven import issue: error: package com.google.gson does not exist 编译错误-在Redhat“ Openshift”应用程序中找不到com.google.gson包 - Compilation error - package com.google.gson not found in Redhat “Openshift” application 错误:包com.google.gson不存在(在playframwork 2.0中) - error: package com.google.gson does not exist (in play framwork 2.0) 在带空格的字符串上使用GSON JsonParser - Using GSON JsonParser on a String with a space 为什么 com.google.gson.Gson::toJson 返回一个空的 ZA8CFDE63311C49EB2AC96B8666? - Why does com.google.gson.Gson::toJson return an empty object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM