简体   繁体   English

android - 从json字符串中提取键值对的简单方法?

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

Suppose I have a json string like this: { ... "key1":"value1"; 假设我有一个像这样的json字符串:{...“key1”:“value1”; ... } with a key1-value1 pair somewhere deep down the json structure (which includes other things such as array, dictionary, etc...). ...}在json结构的内部某处有一个key1-value1对(包括其他内容,如数组,字典等......)。 I don't know exactly (and don't care) how the exact structure of the json is. 我不确切地知道(并且不关心)json的确切结构。 Is there a simple way to extract the "value1" ? 有没有一种简单的方法来提取“value1”? (if there are 2 "key1" in the json string then I just need the first one). (如果json字符串中有2个“key1”,那么我只需要第一个)。

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 ). 如果你真的不知道你期望的JSON字符串的结构是什么,你可以尝试图搜索方法,例如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. 找到钥匙后中断您的流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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