1
我正在使用JsonPath从JSON文件中检索值。 JSON文件看起来是这样的:从JSONArray获取元素时保留双引号
[
{
"username": "John",
"password": {
"passwordValue": "passwordjohn",
"secret_key": "123"
}
},
{
"username": "Nick",
"password": {
"passwordValue": "XXX",
"secret_key": "ZZZ",
"other_key": "YYY"
}
}
]
的JsonPath我使用的是从一个特定的用户检索password
。例如:
fun getPassword() {
val passwords: JSONArray = read(jsonFile, "\$.[?(@.name==\"John\")].password")
}
但是,我发现了两个障碍。首先,我始终得到net.minidev.json.JSONArray
,并且附加[0]
的相同路径不起作用。
因此,我尝试从JSONArray
中获得唯一的元素,例如:credentials[0]
。不幸的是,这删除在字段名的双引号,导致这样的事情:
{passwordValue: passwordjohn, secret_key: 123}
这是不可能的工作。
我正在寻找一种方式来获得这种回:
{"passwordValue": "passwordjohn", "secret_key": "123"}
有趣的是,大多数过去的问题都会问如何删除引号:) – notyou
检查此github问题: https://github.com/json-path/JsonPath/issues/275#issue-184313633 – pRaNaY