python - 如何使用python读取json文件并根据ID打印json正文
问题描述
我有一个 json 文件,其数据如下:
{
"A": "ID",
"B": "Priority",
"C": "Match URL",
"D": "Match Query",
"E": "Redirect URL",
"F": "Status Code",
"G": "Descr.",
"H": "Type",
"I": "Mode",
"J": "Preserve Query",
"K": "Start Date",
"L": "End Date",
"M": "Actions"
},
{
"A": "24141",
"B": "",
"C": "/s/l",
"D": "",
"E": "http://local.tb.hello.com/markets/abc.hello.com/mobile/smsLanding.html",
"F": "0",
"G": "",
"H": "String",
"I": "Internal",
"J": "",
"K": "",
"L": "",
"M": "Toggle Dropdown"
},
{
"A": "26875",
"B": "",
"C": "/l",
"D": "",
"E": "http://local.tb.hello.com/markets/abc.hello.com/mobile/smsLanding.html",
"F": "0",
"G": "",
"H": "String",
"I": "Internal",
"J": "",
"K": "",
"L": "",
"M": "Toggle Dropdown"
},
我正在尝试读取上述 json 文件的文件并根据 ID 打印整个正文。例如:如果我输入的 ID 为 24141,我应该得到下面的 json 格式:
{
"A": "24141",
"B": "",
"C": "/s/l",
"D": "",
"E": "http://local.tb.hello.com/markets/abc.hello.com/mobile/smsLanding.html",
"F": "0",
"G": "",
"H": "String",
"I": "Internal",
"J": "",
"K": "",
"L": "",
"M": "Toggle Dropdown"
}
请帮助如何实现这一目标。我是 python 新手,所以尝试读取文件,它可以保存在字符串变量中,但无法将其转换回 json 并基于 id。
解决方案
一个简单的方法。可能有更高性能的路线。
dd = {Define your json file.}
result = None
find_item = '26875'
for d in dd:
item = d.get('A', None)
if find_item == item:
result = d
break
print(result)
推荐阅读
- javascript - 承诺在 Javascript 中无法以同步方式解决问题
- flutter - Flutter、pdf包和表格组件
- java - 如何检查用户何时单击了正确的按钮?
- c# - UWP 应用程序:地图路线 API (MapRouteFinder.GetDrivingRouteAsync) 不响应,甚至无法刷新或取消请求
- laravel - OOP Laravel 解析从 DB 获取的数据
- pine-script - 如何从 Pine Script (Tradingview) 中的特定日期获取绿色和红色音量条计数?
- javascript - 如何将属性直接传递给 vuejs 中的组件,例如 reactjs
- parquet - 如何将 Azure 数据集标记为从具有日期分区的镶木地板文件夹中读取的时间序列数据集?
- unity3d - 如何最好地自动化重复的 Unity 工作流程
- ios - Swift 5- Firebase- 在标题中按日期分组