简体   繁体   中英

Finding text in json with python

I've got a json file that I'm importing into Python and trying to look for the occurrence of a phrase. If I try this:

any(keyword in s for s in json_data)

where keyword is the thing I'm looking for and json_data is what I got from using json.load(). It always returns false even when the keyword is in json_data. Here's how I'm indexing into the json:

json_data["thing1"]["thing2"][0]["thing3"]

The field [0] varies from 0-16 and the thing I want is in thing3. Why can't I get a True even when the keyword is in json_data?

any(keyword in s for s in json_data)

is looking in the first level dictionary, where thing1 is located. You have to look at the dictionary containing thing3 , which is the indexed one

any(keyword in s for s in json_data["thing1"]["thing2"])

Assuming thing3 is the keyword you are looking for...

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