简体   繁体   中英

android - simple way to extract a key-value pair from a json string?

Suppose I have a json string like this: { ... "key1":"value1"; ... } with a key1-value1 pair somewhere deep down the json structure (which includes other things such as array, dictionary, etc...). I don't know exactly (and don't care) how the exact structure of the json is. Is there a simple way to extract the "value1" ? (if there are 2 "key1" in the json string then I just need the first one).

As far as I know, you have no chance of doing it manually. If you really don't know what's the structure of the JSON string you're expecting, you can try a graph search approach, such as DFS ( http://en.wikipedia.org/wiki/Depth-first_search ).

For every key, check if it is an array. If so, go inside and repeat the procedure. If nothing was found in a given array, backtrack. Interrupt your process once you have found your key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM