python+json:解析为lis

2024-05-23 21:45:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我对用python(使用python 2.7)解析JSON数据有些陌生。有一个服务需要发送API调用,JSON响应类似于下面的内容。“行”中的项目数量可能会有所不同。我需要做的是,如果有第二行,只从第二行提取“内容”,并将其放入列表中。从本质上讲,它只是一个“竞选确认号码”的列表,而不是别的。如果有什么帮助的话,这个数字也总是只有9个数字。任何建议都将不胜感激。

{"response":
    {"result":
        {"Potentials":
            {"row":
                [
                {"no":"1","FL":
                    {"content":"523836000004148171","val":"POTENTIALID"}
                },
                {"no":"2","FL":
                    {"content":"523836000004924051","val":"POTENTIALID"}
                },
                {"no":"3","FL":
                    [
                    {"content":"523836000005318448","val":"POTENTIALID"},
                    {"content":"694275295","val":"Campaign Confirmation Number"}
                    ]
                },
                {"no":"4","FL":
                    [
                    {"content":"523836000005318662","val":"POTENTIALID"},
                    {"content":"729545274","val":"Campaign Confirmation Number"}
                    ]
                },
                {"no":"5","FL":
                    [
                    {"content":"523836000005318663","val":"POTENTIALID"},
                    {"content":"903187021","val":"Campaign Confirmation Number"}
                    ]
                },
                {"no":"6","FL":
                    {"content":"523836000005322387","val":"POTENTIALID"}
                },
                {"no":"7","FL":
                    [
                    {"content":"523836000005332558","val":"POTENTIALID"},
                    {"content":"729416761","val":"Campaign Confirmation Number"}
                    ]
                }
                ]
            }
        },
    "uri":"/crm/private/json/Potentials/getSearchRecords"}
}

编辑:此示例的输出示例如下:

confs=[69427529572954527490318702729416761]

或者

confs=['694275295','729545274','903187021','729416761']

它们是否存储为字符串或int并不重要

编辑2:这是我的代码剪辑:

import urllib
import urllib2
import datetime
import json

key = '[removed]'

params = {
    '[removed]'
    }

final_URL = 'https://[removed]'
data = urllib.urlencode(params)
request = urllib2.Request(final_URL,data)
response = urllib2.urlopen(request)
content = response.read()

j = json.load(content)

confs = []
for no in j["response"]["result"]["Potentials"]["row"]:
    data = no["FL"]
    if isinstance(data, list) and len(data) > 1:
        confs.append(int(data[1]["content"]))

print confs

Tags: noimportjsonnumberdataresponsevalcontent
2条回答

假设“response”包含json字符串:

import json

data = json.loads(response)
rows = data['response']['result']['Potentials']['rows']
output = []
for row in rows:
    contents = row['FL']
    if len(contents) > 1:
        output.append(contents[1]['content'])

应该可以。

编辑:

我终于有时间测试这个“一行”。使用Python的功能特性很有趣:

import json

#initialize response to your string
data = json.loads(response)
rows = data['response']['result']['Potentials']['row']
output = [x['FL'][1]['content'] for x in rows if isinstance(x['FL'], list) and len(x['FL']) > 1]
print output

['694275295', '729545274', '903187021', '729416761']

假设j是您的JSON对象,上面的结构已被解析为:

>>> results = []
>>> for no in j["response"]["result"]["Potentials"]["row"]:
...     data = no["FL"]
...     if isinstance(data, list) and len(data) > 1:
...         results.append(int(data[1]["content"]))
...
>>> results
[694275295, 729545274, 903187021, 729416761]

相关问题 更多 >