2017-09-14 7 views
1

JsonPath을 사용하여 JSON 파일에서 값을 검색하고 있습니다. 내가 사용하고JSONArray에서 요소를 가져올 때 큰 따옴표를 사용하십시오.

[ 
    { 
    "username": "John", 
    "password": { 
     "passwordValue": "passwordjohn", 
     "secret_key": "123" 
    } 
    }, 
    { 
    "username": "Nick", 
    "password": { 
     "passwordValue": "XXX", 
     "secret_key": "ZZZ", 
     "other_key": "YYY" 
    } 
    } 
] 

JsonPath 특정 사용자로부터 password를 검색하는 것입니다 : JSON 파일은 다음과 같이 보인다. 예 :

fun getPassword() { 
    val passwords: JSONArray = read(jsonFile, "\$.[?(@.name==\"John\")].password") 
} 

그러나, 나는 두 개의 장애물을 발견했다. 첫째로, 나는 net.minidev.json.JSONArray을 항상 되돌려 놓았고, 동일한 경로에 [0]이 추가되어 작동하지 않습니다.

그러므로 과 같이 돌아 오는 JSONArray에서 유일한 요소를 얻으려고합니다. 불행하게도,이 이 같은 결과 필드 이름에 따옴표를 제거 : 작업하는 것은 불가능

{passwordValue: passwordjohn, secret_key: 123} 

합니다. 나는이 다시 얻을 수있는 방법을 찾고 있어요

는 : 내가하고 결국 무엇

{"passwordValue": "passwordjohn", "secret_key": "123"} 
+1

Funnily 정도로, 지난 질문은 따옴표를 제거하는 방법을 묻습니다. – notyou

+0

이 github 문제를 확인하십시오 : https://github.com/json-path/JsonPath/issues/275#issue-184313633 – pRaNaY

답변

0

String로 변환 후 JSONArray의 시작부터 [] 상징을 제거하는 것이었다 :

private fun JSONArray.toCredentialString(): String { 
    val credentialString = this.toString() 
    return credentialString.substring(1, credentialString.length - 1) 
} 

더 나은 솔루션을 원합니다.