请教 json数据python 提取指定数值的方法
请教 json数据 提取指定数值的方法还没确定的思路提取数值的方法 想到了正则表达式但还不会
源数据:
{
"code" : 1001,
"msg" : "succ",
"result" : {
"paper" : {
"createId" : 1,
"createTs" : "2022-03-08 11:24:42",
"id" : 10048,
"isRandom" : 1,
"name" : "每日答题",
"passScore" : 0,
"publish" : "Y",
"sourceId" : 0,
"sourceType" : 0,
"status" : "Y",
"totalScore" : 5,
"type" : 5,
"updateId" : null,
"updateTs" : null
},
"questions" : {
"answer" : [],
"judge" : [
{
"checkOption" : null,
"context" : null,
"factScore" : null,
"optionList" : [],
"paperId" : 10048,
"questionId" : 496,
"questionTitle" : "判断下面说法是否正确:二氧化碳是火灾中致死的主要燃烧产物之一,能阻碍人体血液中氧气的输送,引起头痛、虚脱、神志不清等症状和肌肉调节障碍等。",
"recordId" : null,
"score" : 1,
"status" : 0,
"trueOption" : "B、错",
"type" : null,
"url" : "F"
}
],
"mutil" : [
{
"checkOption" : null,
"context" : null,
"factScore" : null,
"optionList" : [
{
"content" : "灭火器",
"id" : 1548,
"index" : 0,
"isRight" : null,
"type" : null
},
{
"content" : "报警电话",
"id" : 1549,
"index" : 1,
"isRight" : null,
"type" : null
},
{
"content" : "火灾事故应急照明灯",
"id" : 1550,
"index" : 2,
"isRight" : null,
"type" : null
},
{
"content" : "灯光疏散指示标志",
"id" : 1551,
"index" : 3,
"isRight" : null,
"type" : null
},
{
"content" : "低压电气漏电保护报警器",
"id" : 1552,
"index" : 4,
"isRight" : null,
"type" : null
}
],
"paperId" : 10048,
"questionId" : 469,
"questionTitle" : "公共娱乐场所按规定一般应设置的消防安全设施、设备包括()。",
"recordId" : null,
"score" : 1,
"status" : 0,
"trueOption" : "A,灭火器 B,报警电话 C,火灾事故应急照明灯 D,灯光疏散指示标志 ",
"type" : null,
"url" : "1548,1549,1550,1551,"
}
],
"single" : [
{
"checkOption" : null,
"context" : null,
"factScore" : null,
"optionList" : [
{
"content" : "液化石油气",
"id" : 1335,
"index" : 0,
"isRight" : null,
"type" : null
},
{
"content" : "热水器",
"id" : 1336,
"index" : 1,
"isRight" : null,
"type" : null
},
{
"content" : "天然气",
"id" : 1337,
"index" : 2,
"isRight" : null,
"type" : null
},
{
"content" : "电饭煲",
"id" : 1338,
"index" : 3,
"isRight" : null,
"type" : null
}
],
"paperId" : 10048,
"questionId" : 416,
"questionTitle" : "设置在地下建筑内的公共娱乐场所严禁使用()。",
"recordId" : null,
"score" : 1,
"status" : 0,
"trueOption" : "A,液化石油气 ",
"type" : null,
"url" : "1335,"
},
{
"checkOption" : null,
"context" : null,
"factScore" : null,
"optionList" : [
{
"content" : "液化石油气",
"id" : 1471,
"index" : 0,
"isRight" : null,
"type" : null
},
{
"content" : "蜂窝煤",
"id" : 1472,
"index" : 1,
"isRight" : null,
"type" : null
},
{
"content" : "天然气",
"id" : 1473,
"index" : 2,
"isRight" : null,
"type" : null
},
{
"content" : "电饭煲",
"id" : 1474,
"index" : 3,
"isRight" : null,
"type" : null
}
],
"paperId" : 10048,
"questionId" : 450,
"questionTitle" : "设置在地下建筑内的公共娱乐场所严禁使用()。",
"recordId" : null,
"score" : 1,
"status" : 0,
"trueOption" : "A,液化石油气 ",
"type" : null,
"url" : "1471,"
},
{
"checkOption" : null,
"context" : null,
"factScore" : null,
"optionList" : [
{
"content" : "重大火灾事故",
"id" : 1331,
"index" : 0,
"isRight" : null,
"type" : null
},
{
"content" : "较大火灾事故",
"id" : 1332,
"index" : 1,
"isRight" : null,
"type" : null
},
{
"content" : "特别重大火灾事故",
"id" : 1333,
"index" : 2,
"isRight" : null,
"type" : null
},
{
"content" : "一般火灾事故",
"id" : 1334,
"index" : 3,
"isRight" : null,
"type" : null
}
],
"paperId" : 10048,
"questionId" : 415,
"questionTitle" : "某娱乐城发生火灾,过火面积约300平方米,当场造成7人死亡,3人重伤。3名重伤者分别在火灾事故发生后的第2天、第5天、第8天死亡。该起火灾事故属于()。",
"recordId" : null,
"score" : 1,
"status" : 0,
"trueOption" : "B,较大火灾事故 ",
"type" : null,
"url" : "1332,"
}
]
}
}
}
提取后的数据应该如这般:
[
{
"checkOption" : 1335,
"questionId" : 416,
"status" : 0
},
{
"checkOption" : 1471,
"questionId" : 450,
"status" : 0
},
{
"checkOption" : 1332,
"questionId" : 415,
"status" : 0
},
{
"checkOption" : "1548,1549,1550,1551",
"questionId" : 469,
"status" : 0
},
{
"context" : "F",
"questionId" : 496,
"status" : 0
}
]
好像每个 questionId 靠近的 url 值就能组成提取后的目标 json 同组的 questionId 与 url
怎么在python里面提取 到呢? 本帖最后由 jjjzw 于 2022-9-24 14:06 编辑
用
```python
json.loads()
```
把json数据加载到字典,然后直接用key取值,分别循环从
`json["result"]["questions"]["judge"]`,
`json["result"]["questions"]["multi"]`,
`json["result"]["questions"]["single"]`
中取 res = {
"code" : 1001,
"msg" : "succ",
"result" : {
"paper" : {
"createId" : 1,
"createTs" : "2022-03-08 11:24:42",
"id" : 10048,
"isRandom" : 1,
"name" : "每日答题",
"passScore" : 0,
"publish" : "Y",
"sourceId" : 0,
"sourceType" : 0,
"status" : "Y",
"totalScore" : 5,
"type" : 5,
"updateId" : None,
"updateTs" : None
},
"questions" : {
"answer" : [],
"judge" : [
{
"checkOption" : None,
"context" : None,
"factScore" : None,
"optionList" : [],
"paperId" : 10048,
"questionId" : 496,
"questionTitle" : "判断下面说法是否正确:二氧化碳是火灾中致死的主要燃烧产物之一,能阻碍人体血液中氧气的输送,引起头痛、虚脱、神志不清等症状和肌肉调节障碍等。",
"recordId" : None,
"score" : 1,
"status" : 0,
"trueOption" : "B、错",
"type" : None,
"url" : "F"
}
],
"mutil" : [
{
"checkOption" : None,
"context" : None,
"factScore" : None,
"optionList" : [
{
"content" : "灭火器",
"id" : 1548,
"index" : 0,
"isRight" : None,
"type" : None
},
{
"content" : "报警电话",
"id" : 1549,
"index" : 1,
"isRight" : None,
"type" : None
},
{
"content" : "火灾事故应急照明灯",
"id" : 1550,
"index" : 2,
"isRight" : None,
"type" : None
},
{
"content" : "灯光疏散指示标志",
"id" : 1551,
"index" : 3,
"isRight" : None,
"type" : None
},
{
"content" : "低压电气漏电保护报警器",
"id" : 1552,
"index" : 4,
"isRight" : None,
"type" : None
}
],
"paperId" : 10048,
"questionId" : 469,
"questionTitle" : "公共娱乐场所按规定一般应设置的消防安全设施、设备包括()。",
"recordId" : None,
"score" : 1,
"status" : 0,
"trueOption" : "A,灭火器 B,报警电话 C,火灾事故应急照明灯 D,灯光疏散指示标志 ",
"type" : None,
"url" : "1548,1549,1550,1551,"
}
],
"single" : [
{
"checkOption" : None,
"context" : None,
"factScore" : None,
"optionList" : [
{
"content" : "液化石油气",
"id" : 1335,
"index" : 0,
"isRight" : None,
"type" : None
},
{
"content" : "热水器",
"id" : 1336,
"index" : 1,
"isRight" : None,
"type" : None
},
{
"content" : "天然气",
"id" : 1337,
"index" : 2,
"isRight" : None,
"type" : None
},
{
"content" : "电饭煲",
"id" : 1338,
"index" : 3,
"isRight" : None,
"type" : None
}
],
"paperId" : 10048,
"questionId" : 416,
"questionTitle" : "设置在地下建筑内的公共娱乐场所严禁使用()。",
"recordId" : None,
"score" : 1,
"status" : 0,
"trueOption" : "A,液化石油气 ",
"type" : None,
"url" : "1335,"
},
{
"checkOption" : None,
"context" : None,
"factScore" : None,
"optionList" : [
{
"content" : "液化石油气",
"id" : 1471,
"index" : 0,
"isRight" : None,
"type" : None
},
{
"content" : "蜂窝煤",
"id" : 1472,
"index" : 1,
"isRight" : None,
"type" : None
},
{
"content" : "天然气",
"id" : 1473,
"index" : 2,
"isRight" : None,
"type" : None
},
{
"content" : "电饭煲",
"id" : 1474,
"index" : 3,
"isRight" : None,
"type" : None
}
],
"paperId" : 10048,
"questionId" : 450,
"questionTitle" : "设置在地下建筑内的公共娱乐场所严禁使用()。",
"recordId" : None,
"score" : 1,
"status" : 0,
"trueOption" : "A,液化石油气 ",
"type" : None,
"url" : "1471,"
},
{
"checkOption" : None,
"context" : None,
"factScore" : None,
"optionList" : [
{
"content" : "重大火灾事故",
"id" : 1331,
"index" : 0,
"isRight" : None,
"type" : None
},
{
"content" : "较大火灾事故",
"id" : 1332,
"index" : 1,
"isRight" : None,
"type" : None
},
{
"content" : "特别重大火灾事故",
"id" : 1333,
"index" : 2,
"isRight" : None,
"type" : None
},
{
"content" : "一般火灾事故",
"id" : 1334,
"index" : 3,
"isRight" : None,
"type" : None
}
],
"paperId" : 10048,
"questionId" : 415,
"questionTitle" : "某娱乐城发生火灾,过火面积约300平方米,当场造成7人死亡,3人重伤。3名重伤者分别在火灾事故发生后的第2天、第5天、第8天死亡。该起火灾事故属于()。",
"recordId" : None,
"score" : 1,
"status" : 0,
"trueOption" : "B,较大火灾事故 ",
"type" : None,
"url" : "1332,"
}
]
}
}
}
ans = []
for single in res["result"]["questions"]["single"]:
ans.append({"checkOption":single["url"],"questionId":single["questionId"],"status":single["status"]})
for mutil in res["result"]["questions"]["mutil"]:
ans.append({"checkOption":mutil["url"],"questionId":mutil["questionId"],"status":mutil["status"]})
for judge in res["result"]["questions"]["judge"]:
ans.append({"context":judge["url"],"questionId":judge["questionId"],"status":judge["status"]})
print(ans)
首先python中不是null,你取值不是用python获取的吧,requests之后应该是None。
然后单选我是按照上面的格式'1335,'写的,不是你给的1335的int格式,你要int格式的话,自己再改下就可以了 jsonpath正解
查看https://www.52pojie.cn/forum.php?mod=viewthread&tid=1695745
页:
[1]