吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 947|回复: 3
收起左侧

[求助] 请教 json数据python 提取指定数值的方法

[复制链接]
plaodj 发表于 2022-9-24 13:54
请教 json数据 提取指定数值的方法

还没确定的思路提取数值的方法   想到了正则表达式  但还不会

源数据:
[Plain Text] 纯文本查看 复制代码
{
   "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,"
            }
         ]
      }
   }
}


提取后的数据应该如这般:
[Plain Text] 纯文本查看 复制代码
[
   {
      "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:04
本帖最后由 jjjzw 于 2022-9-24 14:06 编辑

json.loads()

把json数据加载到字典,然后直接用key取值,分别循环从
json["result"]["questions"]["judge"]
json["result"]["questions"]["multi"]
json["result"]["questions"]["single"]
中取

youth96 发表于 2022-9-24 14:08
[Python] 纯文本查看 复制代码
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格式的话,自己再改下就可以了
playboysen 发表于 2022-10-8 22:24
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 06:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表