JAVAlang.NumberFormatException:用于尝试读取包含数字的路径的Json时的输入字符串
我试图返回一个JSON数组,如下所示:
jsarr = new JSONArray(JSON.read(genericPathComp).toString());
其中,genericPathComp是一个已编译的JSON路径
这种方法很好,但当我的JSON路径包含一个数字时,会引发以下异常:
java.lang.NumberFormatException: For input string: "ustomerAccountBalance,billingAccount[(-1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at com.jayway.jsonpath.internal.filter.ArrayIndexFilter.filter(ArrayIndexFilter.java:75)
at com.jayway.jsonpath.internal.filter.PathTokenFilter.filter(PathTokenFilter.java:50)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:255)
at com.jayway.jsonpath.internal.JsonReader.read(JsonReader.java:103)
at dataAnalytics.ValidateAttributes.responseParser(ValidateAttributes.java:174)
at dataGeneration.Main.main(Main.java:76)
引发此异常的JSON路径为: $.parts[客户账户余额,账单账户[-1:]
我不明白它为什么要解析一个int,我遗漏了什么
编辑:我使用的是jayway解析器,变量JSON是一个ReadContext变量。所以它是一个有效JSON的句柄。到目前为止,这段代码适用于所有没有数字的JSON路径。例如美元。部分。billingAccount和$。名称把所有的工作都记录下来
最初的JSON如下所示:
[
{
"Name": [
{
"value": "John"
}
],
"parts": {
"customerAccountBalance": [
{
"amount": "1"
}
],
"billingAccount": [
{
"type": "example"
},
{
"date": "also example"
}
]
}
}
]
我想返回一个数组,它由最后一个账单账户数组和所有客户账户余额组成
# 1 楼答案
这个例外
。。。之所以发生,是因为Jayway希望在开头的方括号后面有一个数字(
0 - 9
)。查看Jayway docs它允许一个开头的方括号后面跟着'
(对于用括号表示的子对象)或?
(对于过滤器),除此之外,它还希望一个开头的方括号后面跟着一个数字因此,问题中提供的json路径无效。我怀疑你是想读
parts.customerAccountBalance
中的最后一个billingAccount
。如果是这样的话,请继续阅读鉴于此:
此json路径:
$.parts.customerAccountBalance.billingAccount[-1:]
将返回:你可以自己用在线Jayway Evaluator试试
更新更新:
鉴于此:
这涉及两个读取调用:
$.billingAccount[-1:]
$.parts.customerAccountBalance