我尝试解析JSON结构并查找特定值。
这里是要解析的可能数据的示例:
{
"objects": [
{
"from": 44,
"my_ref": "http://192.168.10.1/index.php",
"allow": "no",
"to": 10
},
{
"from": 20,
"my_ref": "http://192.168.10.2/index.php",
"allow": "mandatory",
"to": 0
}
],
"comment": "My PHP",
"identifiable_with_user": true,
"key": 10,
"link": [
{
"href": "http://192.168.10.1/index.php",
"method": "GET",
"rel": "self",
"type": "website"
},
{
"href": "http://192.168.10.5/identifiable.php",
"method": "GET",
"rel": "account_info"
}
],
"name": "Accounts",
"read_only": true,
"system": true,
"system_key": 20,
"tls_match_ref": "http://192.168.10.5/accounts.php"
}
我需要在通用JSON结构中提取由key * ref标识的所有值(如my_ref,href等...)。我只需要提取URL然后执行一些操作:
for entities in JSON_structure
URL= "take the URL corresponding to the ref key"
so something with the URL
我尝试使用.items()过滤" http://"字符串,但没有工作
URL = {k:v for (k,v) in element_detail.items() if "http://" in k or v}
答案 0 :(得分:0)
您应该解析数据,然后查找带有ref的键。
这就是诀窍:
import json
def parse(root):
for k,v in root.items():
if isinstance(v, list):
for e in v:
parse(e)
else:
if 'ref' in k:
print(k + ':' + v)
json_data = '{\
"objects": [\
{ "from": 44, "my_ref": "http://192.168.10.1/index.php", "allow": "no", "to": 10 },\
{ "from": 20, "my_ref": "http://192.168.10.2/index.php", "allow": "mandatory", "to": 0 }\
],\
"comment": "My PHP",\
"identifiable_with_user": true,\
"key": 10,\
"link": [\
{ "href": "http://192.168.10.1/index.php", "method": "GET", "rel": "self", "type": "website" },\
{ "href": "http://192.168.10.5/identifiable.php", "method": "GET", "rel": "account_info" }\
],\
"name": "Accounts",\
"read_only": true,\
"system": true,\
"system_key": 20,\
"tls_match_ref": "http://192.168.10.5/accounts.php"\
}'
data = json.loads(json_data)
parse(data)
输出:
href:http://192.168.10.1/index.php
href:http://192.168.10.5/identifiable.php
my_ref:http://192.168.10.1/index.php
my_ref:http://192.168.10.2/index.php
tls_match_ref:http://192.168.10.5/accounts.php