英文:
In Rest Assured, getString() round off the decimal values after 4 digit
问题
我有一个场景,我们需要从 JSON 响应体中读取纬度和经度。
纬度和经度的值如下所示:
"data": [{
"city": "Mumbai",
"country": "India",
"latitude": 19.0886993408,
"longitude": 72.8678970337
}]
当我们尝试通过使用 JSONPATH 中的 getString()
来读取纬度和经度来验证 JSON 响应时,它会在小数位数后四舍五入为 4 位。
以下是 Java 代码:
String key = item.getKey();
String value = item.getValue();
JsonPath jsonPathEvaluator = response.jsonPath();
String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", "");
输出显示:
纬度:-19.0887,而不是 19.0886993408
英文:
I have scenario, we need to read the latitude and Longitude from json response body.
The latitude and longitude has value like below
"data": [{
"city": "Mumbai",
"country": "India",
"latitude": 19.0886993408,
"longitude": 72.8678970337
}
when We try to validate the JSON response by reading the latitude and longitude using JSONPATH
using getString()
then it round off the value after 4 digit in decimal places.
Below is the code in java
String key = item.getKey();
String value = item.getValue();
JsonPath jsonPathEvaluator = response.jsonPath();
String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", "");
Output display:
Latitude:-19.0887 instead of 19.0886993408
答案1
得分: 1
你可以尝试使用如下所述的 JsonPath.config:
使用它,您可以为 JsonPath 提供自定义配置。
在这里,我只是试图将我的数值数据转换为具有长十进制范围的双精度数。
String key = item.getKey();
String value = item.getValue();
JsonPath jsonPathEvaluator = response.jsonPath();
JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);
String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", "");
/* String json = "{
"data": {
"city": "Mumbai",
"country": "India",
"latitude": 19.0886993408,
"longitude": 72.8678970337
}
}";
JsonPath jsonPath = JsonPath.from(json);
JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);
double strjson = jsonPath.get("data.latitude");
System.out.println(String.valueOf(strjson)); */
对我来说,它会打印出以下输出。
19.0886993408
英文:
You can try with JsonPath.config as mentioned here
Using it you can provide your custom configuration for JsonPath.
Here i am just trying to convert my numeric data with long decimal range double.
String key = item.getKey();
String value = item.getValue();
JsonPath jsonPathEvaluator = response.jsonPath();
JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);
String strjson = jsonPathEvaluator.getString(key).toString().replaceAll("\\[\\](){}]", "");
/* String json = "{\n" +
" \"data\": {\n" +
" \"city\": \"Mumbai\",\n" +
" \"country\": \"India\",\n" +
" \"latitude\": 19.0886993408,\n" +
" \"longitude\": 72.8678970337\n" +
" }\n" +
"}";
JsonPath jsonPath = JsonPath.from(json);
JsonPath.config = new JsonPathConfig(JsonPathConfig.NumberReturnType.DOUBLE);
double strjson = jsonPath.get("data.latitude");
System.out.println(String.valueOf(strjson)); */
For me it's printing below output.
19.0886993408
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论