实现目的:json文件中存在非标准格式的json字符串,实现合并和更改。
原json文件格式:
{
"return_code": "0",
"return_msg": null,
"msg_id": "20240201170018",
"response_data":
"[
{\"create_time\":\"2024-01-30 16:32:58\",\"full_marks\":\"\",\"modify_time\":\"\",\"visit_implement_id\":\"6098427624796267753\",\"score\":\"\",\"sts\":[],\"modifyier_id\":\"-1\",\"creator_id\":\"7813335763439853717\",\"id\":\"6767716655336023621\",\"slfdf_2011110001\":\"\u662f\",\"slfdf_2011110006\":\"\",\"source_code\":\"\",\"slfdf_2011110007\":\"\",\"status\":\"1\"},
{\"create_time\":\"2024-01-29 16:01:06\",\"full_marks\":\"\",\"modify_time\":\"\",\"visit_implement_id\":\"7230970174028779390\",\"score\":\"\",\"sts\":[],\"modifyier_id\":\"-1\",\"creator_id\":\"4856698126659739508\",\"id\":\"7647950391882326107\",\"slfdf_2011110002\":\"\u662f\",\"slfdf_2011110007\":\"\",\"source_code\":\"\",\"slfdf_2011110007\":\"\",\"status\":\"1\"},
{\"create_time\":\"2024-01-18 13:53:40\",\"full_marks\":\"\",\"modify_time\":\"\",\"visit_implement_id\":\"5645585817796944565\",\"score\":\"\",\"sts\":[],\"modifyier_id\":\"-1\",\"creator_id\":\"6193840498282836054\",\"id\":\"6224160597124499535\",\"slfdf_2011110003\":\"\u662f\",\"slfdf_2011110008\":\"\",\"source_code\":\"\",\"slfdf_2011110007\":\"\",\"status\":\"1\"},
{\"create_time\":\"2024-01-18 13:51:20\",\"full_marks\":\"\",\"modify_time\":\"\",\"visit_implement_id\":\"6172394703576066883\",\"score\":\"\",\"sts\":[],\"modifyier_id\":\"-1\",\"creator_id\":\"6193840498282836054\",\"id\":\"8855888484451597555\",\"slfdf_2011110004\":\"\u662f\",\"slfdf_2011110009\":\"\",\"source_code\":\"\",\"slfdf_2011110007\":\"\",\"status\":\"1\"}
]"
}
以上响应信息是错误的,不是标准的json字符串格式。
json嵌套格式示例:
{
"name":"jack",
"age":29,
"hobby":["reading","travel","photography"]
"school":{
"name":"Merrimack College",
"location":'North Andover, MA'
}
}
*可以注意到,在嵌套列表时,在方括号[]前后没有双引号,在引号前也不需要转义字符 正确格式:
{
"return_code": "0",
"return_msg": null,
"msg_id": "20240201170018",
"response_data":
[
{ "create_time ": "2024-01-30 16:32:58 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "6098427624796267753 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "7813335763439853717 ", "id ": "6767716655336023621 ", "slfdf_2011110001 ": " u662f ", "slfdf_2011110006 ": " ", "source_code ": " ", "slfdf_2011110007 ": " ", "status ": "1 "},
{ "create_time ": "2024-01-29 16:01:06 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "7230970174028779390 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "4856698126659739508 ", "id ": "7647950391882326107 ", "slfdf_2011110002 ": " u662f ", "slfdf_2011110007 ": " ", "source_code ": " ", "slfdf_2011110007 ": " ", "status ": "1 "},
{ "create_time ": "2024-01-18 13:53:40 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "5645585817796944565 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "6193840498282836054 ", "id ": "6224160597124499535 ", "slfdf_2011110003 ": " u662f ", "slfdf_2011110008 ": " ", "source_code ": " ", "slfdf_2011110007 ": " ", "status ": "1 "},
{ "create_time ": "2024-01-18 13:51:20 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "6172394703576066883 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "6193840498282836054 ", "id ": "8855888484451597555 ", "slfdf_2011110004 ": " u662f ", "slfdf_2011110009 ": " ", "source_code ": " ", "slfdf_2011110007 ": " ", "status ": "1 "}
]
}
这里使用了字符串的替换:
response_data=data.replace('"[', '[').replace(']"', ']').replace('\\"', '"')
要实现的功能:
在“response_data”字段里有多条记录,每条记录里存在多个以slfdf开头的键值,比如说slfdf_2011110001,在每一条记录中将这些以slfdf开头的键值合并到一个列表slfdf中,最后json文件呈现出如图1所示的格式。
完整代码:
import json
# response_data=data.replace('"[', '[').replace(']"', ']').replace('\\"', '"')
data='''
{
"return_code": "0",
"return_msg": null,
"msg_id": "20240201170018",
"response_data":
[
{ "create_time ": "2024-01-30 16:32:58 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "6098427624796267753 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "7813335763439853717 ", "id ": "6767716655336023621 ", "slfdf_2011110001": " u662f ", "slfdf_2011110006": " ", "source_code": " ", "slfdf_2011110007": " ", "status ": "1 "},
{ "create_time ": "2024-01-29 16:01:06 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "7230970174028779390 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "4856698126659739508 ", "id ": "7647950391882326107 ", "slfdf_2011110002": " u662f ", "slfdf_2011110007": " ", "source_code": " ", "slfdf_2011110007": " ", "status ": "1 "},
{ "create_time ": "2024-01-18 13:53:40 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "5645585817796944565 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "6193840498282836054 ", "id ": "6224160597124499535 ", "slfdf_2011110003": " u662f ", "slfdf_2011110008": " ", "source_code": " ", "slfdf_2011110007": " ", "status ": "1 "},
{ "create_time ": "2024-01-18 13:51:20 ", "full_marks ": " ", "modify_time ": " ", "visit_implement_id ": "6172394703576066883 ", "score ": " ", "sts ":[], "modifyier_id ": "-1 ", "creator_id ": "6193840498282836054 ", "id ": "8855888484451597555 ", "slfdf_2011110004": " u662f ", "slfdf_2011110009": " ", "source_code": " ", "slfdf_2011110007": " ", "status ": "1 "}
]
}
'''
#json字符串转换为 Python 格式
trans_data=json.loads(data)
for record in trans_data["response_data"]:# 遍历每一条记录
slfdf = []
temp_dict = {} # 用于保存不以'sldfd'开头的键值对
for key, value in record.items():#在每一条记录里面实现slfdf合并成一个列表,列表里是不同的字典
if key.startswith('slfdf'):
dict_entry = {"key": key, "value": value}
slfdf.append(dict_entry)
else:
temp_dict[key] = value # 将不以'sldfd'开头的键值对保存到临时字典中
temp_dict.update({"slfdf": slfdf})
record.clear()
record.update(temp_dict)
response_data=json.dumps(trans_data, indent=4)
print(response_data)