##例:
aaaa = {"beginPageIndex": 1, "currentPage": 1, "endPageIndex": 1, "pageCount": 1, "pageSize": 20, "recordCount": 1,
"recordList": [{"date": "2009-05-07 14:51:11.000",
"abstra": "随着7月29日“法拉加特”号导弹驱逐舰和“考夫曼”号导弹护卫舰在第40驱逐舰分舰队指挥官的指挥下,驶出梅波特海军基地,“美洲伙伴关系-2008”演习开始了。参加演习的各国海军兵力被编成特混编队(CTF),美国第3舰队副司令塞缪尔·洛克里尔海军中将被任命为编队指挥官直接指挥演习。",
"picurl": "", "nodename": "焦点新闻", "author": "", "sourcename": "", "id": "bJHxGG0BPLwr9UCVDe-m",
"title": "专家总结美海军08年训练重点 强化海上安保行动",
"content": "然后举行了美韩“关键决心““鹞鹰”演习。“俄亥俄”号战略核潜艇在其在西太平洋为期15个月的巡逻的最后阶段参加了演习。艇上作战指挥中心对演习中的美、韩海军特种作战部队的行动进行协调。",
"url": "http://www.china.com.cn/military/txt/2009-05/07/content_17739183.htm"}],
"tookSecond": 0.029}
bbb = AnalyzeJsonData(aaaa)
data = "//recordList/date/text()"
ccc = bbb.ad(data)
print(ccc)
结果

class JsonNode(object):
def __init__(self, json_data):
self.json_data = json_data
def obj_json(self):
return self.json_data
def ad(self):
pass
class AnalyzeJsonData(object):
def __init__(self, json_data):
""""""
self.json_data = JsonNode(json_data)
# 分解解析规则
def resolve_rule(self, analyze_rule):
""""""
if isinstance(analyze_rule, str):
resolve_res = analyze_rule.lstrip("//").split("/")
if resolve_res[-1] == "text()":
finally_rule = resolve_res.pop(-1)
# elif resolve_res[-1] == "data":
# finally_rule = resolve_res.pop(-1)
else:
finally_rule = ""
return resolve_res, finally_rule
else:
raise ValueError(f"解析规则必需是【str】类型")
# 解析数据
def ad(self, analyze_rule):
""""""
analyze_rules, finally_rule = self.resolve_rule(analyze_rule)
# ['hotList', 'itemList', 'itemImageNew']
# 解析规则被分为3层,
json_datas = [self.json_data]
# 对解析规则进行循环
for rule_index, rule_data in enumerate(analyze_rules):
# 将字典组成列表的形式,字典的键不能重复,所以获取到值后便是结果
if rule_data:
new_json_datas = list()
for json_data in json_datas:
child_node = json_data.obj_json()[rule_data]
if isinstance(child_node, list):
new_child_list = list()
for new_child in child_node:
new_child_list.append(JsonNode(new_child))
new_json_datas = new_child_list
else:
child_node_obj = JsonNode(child_node)
new_json_datas.append(child_node_obj)
json_datas = new_json_datas
else:
return ""
if finally_rule == "text()":
datas_res = list()
for data_res in json_datas:
datas_res.append(data_res.obj_json())
return datas_res
else:
datas_res = list()
for json_node in json_datas:
datas_res.append(AnalyzeJsonData(json_node.obj_json()))
return datas_res
2009年的“美洲伙伴关系-2008”演习由美国海军主导,涉及多国海军兵力,旨在强化海上安保行动。演习开始时,“法拉加特”号和“考夫曼”号导弹舰离港,美国第3舰队副司令塞缪尔·洛克里尔担任指挥。演习中还包括了美韩“关键决心”和“鹞鹰”演习,展示了各国海军的协同作战能力。

2977

被折叠的 条评论
为什么被折叠?



