解析整个JSON对象,在python中查找键

时间:2014-11-16 07:06:59

标签: python json

我试图查找某个键是否在json对象中。我用

创建了json
googleRequest = json.loads(googleRequest.content) # its a google api call

json并不总是以相同的方式格式化,我想找到某个键​​是否在json中的某个位置。我试过用这个:

if "----" is in jsonObject:
    do stuff

然而,这仅在密钥位于json的第一级时才有效。有没有办法解析通过json对象寻找某个键,无论键位于何处?

这是json的一个不完整的模拟:

{
 "kind": "books#volumes",
 "totalItems": 1,
 "items": [
  {
   "kind": "books#volume",
   "id": "HDvHjwEACAAJ",
   "etag": "+2K7d2N2VNg",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/HDvHjwEACAAJ",
   "volumeInfo": {
    "title": "Fahrenheit 451",
    "authors": [
     "Ray Bradbury"
    ],
    "publisher": "Voyager",
    "publishedDate": "2013",
    "description": "The terrifyingly prophetic novel of a post-literate future Guy Montag is a fireman. His job is to destroy the most illegal of commodities, the source of all discord and unhappiness, the printed book.",

我希望能够找到标题,即使它位于“kind”或“volumeInfo”之下并保存其值。

编辑:

我有这个功能:

def find_key(dic, key_match):
  keys=[]
  values=[]
  if isinstance(dic,dict):
    for key,value in dic.items():
      if isinstance(value,dict):
        keys.append(key)
        keys.append(find_key(value, key_match))
      elif isinstance(value,list):
        keys.append(key)
        keys.append(find_key(value[0], key_match))
      else:
        keys.append(key)
        if key == key_match:
          print value #PRINTS OUT WHAT I WANT
          values.append(value)
   return values # in line with for loop

我打印出来的打印声明打印出我想要的内容,但我无法获得返回该值的功能。它只返回空[]。

4 个答案:

答案 0 :(得分:0)

以递归方式构建一个迭代JSON对象的函数:

  • 如果密钥位于当前级别,则返回其值。
  • 如果没有,请运行所有内部词典的功能。

答案 1 :(得分:0)

您可以使用嵌套函数循环遍历dictionary项并每次检查值的类型,如果类型为listdict您再次调用该函数,否则您将密钥附加到总密钥列表中:

def find_key(dic):
 keys=[]
 if isinstance(dic,dict): 
  for key,value in dic.items():
     if isinstance(value,dict):
        keys.append(key)
        keys.append(find_key(value))
     elif isinstance(value,list):
         keys.append(key)
         keys.append(find_key(value[0]))
     else:
        keys.append(key)
  return keys

结果:

['totalItems', 'items', ['kind', 'etag', 'volumeInfo', ['publisher', 'publishedDate', 'authors', None, 'description', 'title'], 'id', 'selfLink'], 'kind']

答案 2 :(得分:0)

for python 3

# python 3 only
def find_values_from_key(key, json_object):
    if isinstance(json_object, list):
        for list_element in json_object:
            yield from find_values_from_key(key, list_element)
    elif isinstance(json_object, dict):
        if key in json_object:
            yield json_object[key]
        for dict_value in json_object.values():
            yield from find_values_from_key(key, dict_value)

for python 2

# python 2 only
def find_values_from_key(key, json_object):
    if isinstance(json_object, list):
        for list_element in json_object:
            for res in find_values_from_key(key, list_element):
                yield res
    elif isinstance(json_object, dict):
        if key in json_object:
            yield json_object[key]
        for dict_value in json_object.values():
            for res in find_values_from_key(key, dict_value):
                yield res

答案 3 :(得分:0)

"title":\s*"([^"]+)"

尝试使用re是其他不行的事情。参见演示。

http://regex101.com/r/lZ5mN8/3

import re
p = re.compile(ur'"title":\s*"([^"]+)"')
test_str = u"{\n \"kind\": \"books#volumes\",\n \"totalItems\": 1,\n \"items\": [\n {\n \"kind\": \"books#volume\",\n \"id\": \"HDvHjwEACAAJ\",\n \"etag\": \"+2K7d2N2VNg\",\n \"selfLink\": \"https://www.googleapis.com/books/v1/volumes/HDvHjwEACAAJ\",\n \"volumeInfo\": {\n \"title\": \"Fahrenheit 451\",\n \"authors\": [\n \"Ray Bradbury\"\n ],\n \"publisher\": \"Voyager\",\n \"publishedDate\": \"2013\",\n \"description\": \"The terrifyingly prophetic novel of a post-literate future Guy Montag is a fireman. His job is to destroy the most illegal of commodities, the source of all discord and unhappiness, the printed book.\",\n\n\n\n\n\n <div style=\"padding-left:2em;\">\n &bull; Location: \n\n Northern Virginia, ☎ 202-210-5936\n\n </div>\n\n\n\n\n <div style=\"padding-left:2em;\"><br />&bull; Post ID: 1234567 washingtondc</div>\n\n\n <div id=\"OtherAdsByThisUser\" data-oid=\"7654321\">"

re.findall(p, test_str)