从 json 数据中获取 id 而无需线性扫描
Get id from json data without linear scan
我有以下 json 数据:
[
{
"date_created": "2019-12-25 12:57:58",
"date_updated": "2020-12-18 16:18:29",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 2,
"id": 1,
"image_path": "",
"on_discount": false,
"price": 20,
"title": "The Art of Scala"
},
{
"date_created": "2019-03-16 05:15:39",
"date_updated": "2020-12-29 08:40:44",
"description": "Agile practices discover requirements and develop solutions through collaborative effort.",
"discount_price": 3,
"id": 2,
"image_path": "",
"on_discount": true,
"price": 30,
"title": "The Joy of Agile"
},
{
"date_created": "2020-09-13 14:40:39",
"date_updated": "2020-09-23 10:52:39",
"description": "Pages is a word processor developed by Apple Inc.",
"discount_price": 5,
"id": 3,
"image_path": "",
"on_discount": false,
"price": 50,
"title": "Talks About Pages"
},
{
"date_created": "2020-07-20 17:51:23",
"date_updated": "2020-08-04 12:06:58",
"description": "Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft.",
"discount_price": 2,
"id": 4,
"image_path": "",
"on_discount": false,
"price": 20,
"title": "This Is A Course About Visual Studio"
},
{
"date_created": "2020-07-04 01:02:49",
"date_updated": "2021-01-31 15:07:20",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 3,
"id": 5,
"image_path": "",
"on_discount": true,
"price": 30,
"title": "Even A Kid Can Learn Scala!"
}]
我想使用键 'id' 从数据中获取一条记录。我是按以下方式做的:
for i in data_id:#data_id is the json objects
if i['id']==id:
print(i)
没有线性扫描我怎么能做到这一点。好像有大量数据,最后一个变量需要花费大量时间。所以,我希望所有变量的访问时间都相同
为什么不使用 Pandas?
df = pd.DataFrame(your_json_data)
display(df)
打印出来:
date_created date_updated description discount_price id image_path on_discount price title
0 2019-12-25 12:57:58 2020-12-18 16:18:29 Scala is a multi-paradigm, general-purpose pro... 2 1 False 20 The Art of Scala
1 2019-03-16 05:15:39 2020-12-29 08:40:44 Agile practices discover requirements and deve... 3 2 True 30 The Joy of Agile
2 2020-09-13 14:40:39 2020-09-23 10:52:39 Pages is a word processor developed by Apple Inc. 5 3 False 50 Talks About Pages
3 2020-07-20 17:51:23 2020-08-04 12:06:58 Microsoft Visual Studio is an integrated devel... 2 4 False 20 This Is A Course About Visual Studio
4 2020-07-04 01:02:49 2021-01-31 15:07:20 Scala is a multi-paradigm, general-purpose pro... 3 5 True 30 Even A Kid Can Learn Scala!
现在您只需 select 通过 id
和 df[df.id =="id"]
。
为此使用 json 包:
mydict = json.loads(myjson)
outdict = [x for x in mydict if x['id'] == id]
我有以下 json 数据:
[
{
"date_created": "2019-12-25 12:57:58",
"date_updated": "2020-12-18 16:18:29",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 2,
"id": 1,
"image_path": "",
"on_discount": false,
"price": 20,
"title": "The Art of Scala"
},
{
"date_created": "2019-03-16 05:15:39",
"date_updated": "2020-12-29 08:40:44",
"description": "Agile practices discover requirements and develop solutions through collaborative effort.",
"discount_price": 3,
"id": 2,
"image_path": "",
"on_discount": true,
"price": 30,
"title": "The Joy of Agile"
},
{
"date_created": "2020-09-13 14:40:39",
"date_updated": "2020-09-23 10:52:39",
"description": "Pages is a word processor developed by Apple Inc.",
"discount_price": 5,
"id": 3,
"image_path": "",
"on_discount": false,
"price": 50,
"title": "Talks About Pages"
},
{
"date_created": "2020-07-20 17:51:23",
"date_updated": "2020-08-04 12:06:58",
"description": "Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft.",
"discount_price": 2,
"id": 4,
"image_path": "",
"on_discount": false,
"price": 20,
"title": "This Is A Course About Visual Studio"
},
{
"date_created": "2020-07-04 01:02:49",
"date_updated": "2021-01-31 15:07:20",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 3,
"id": 5,
"image_path": "",
"on_discount": true,
"price": 30,
"title": "Even A Kid Can Learn Scala!"
}]
我想使用键 'id' 从数据中获取一条记录。我是按以下方式做的:
for i in data_id:#data_id is the json objects
if i['id']==id:
print(i)
没有线性扫描我怎么能做到这一点。好像有大量数据,最后一个变量需要花费大量时间。所以,我希望所有变量的访问时间都相同
为什么不使用 Pandas?
df = pd.DataFrame(your_json_data)
display(df)
打印出来:
date_created date_updated description discount_price id image_path on_discount price title
0 2019-12-25 12:57:58 2020-12-18 16:18:29 Scala is a multi-paradigm, general-purpose pro... 2 1 False 20 The Art of Scala
1 2019-03-16 05:15:39 2020-12-29 08:40:44 Agile practices discover requirements and deve... 3 2 True 30 The Joy of Agile
2 2020-09-13 14:40:39 2020-09-23 10:52:39 Pages is a word processor developed by Apple Inc. 5 3 False 50 Talks About Pages
3 2020-07-20 17:51:23 2020-08-04 12:06:58 Microsoft Visual Studio is an integrated devel... 2 4 False 20 This Is A Course About Visual Studio
4 2020-07-04 01:02:49 2021-01-31 15:07:20 Scala is a multi-paradigm, general-purpose pro... 3 5 True 30 Even A Kid Can Learn Scala!
现在您只需 select 通过 id
和 df[df.id =="id"]
。
为此使用 json 包:
mydict = json.loads(myjson)
outdict = [x for x in mydict if x['id'] == id]