鱼C论坛

 找回密码
 立即注册
查看: 898|回复: 4

python json

[复制链接]
发表于 2019-5-1 10:19:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
"jsonObject": {
        "comments": [
            {
                "attachments": [
                    
                ],
                "comments": [
                    
                ],
                "create_time": 1556625481348,
                "ip": "",
                "comment_id": 150423347,
                "reply_count": 0,
                "content": "我想问一下他们有善心嘛",
                "ip_location": "广东省广州市",
                "reply_id": 0,
                "passport": {
                    "img_url": "https://sucimg.itc.cn/avatarimg/296670289_1444181118790",
                    "user_id": 10115405,
                    "nickname": "鱼可以飞么"
                },
                "user_id": 10115405,
                "from": "",
                "support_count": 0,
                "status": 1
            },
            {
                "attachments": [
                    
                ],
                "comments": [
                    
                ],
                "create_time": 1556625475042,
                "ip": "",
                "comment_id": 150423360,
                "reply_count": 0,
                "content": "宇智波鼬的神器",
                "ip_location": "西藏自治区拉萨市",
                "reply_id": 0,
                "passport": {
                    "img_url": "https://edc3f09a7c333.cdn.sohucs.com/s_mini/pic/2018/04/02/1.png",
                    "user_id": 10115964,
                    "nickname": "想个名咋这么难"
                },
                "user_id": 10115964,
                "from": "",
                "support_count": 0,
                "status": 1
            }

我想取到里面passport里的user_id和nickname,这个地方应该怎么写
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-1 15:22:51 | 显示全部楼层
用法和字典一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-1 19:20:12 | 显示全部楼层
  1. # coding: utf-8
  2. import requests
  3. from lxml import etree


  4. if __name__ == '__main__':
  5.   jszd=  { "comments": { "attachments": [],
  6.                  "comments": [],
  7.                  "create_time": 1556625481348,
  8.                  "ip": "",
  9.                  "comment_id": 150423347,
  10.                  "reply_count": 0,
  11.                  "content": "我想问一下他们有善心嘛",
  12.                  "ip_location": "广东省广州市",
  13.                  "reply_id": 0,
  14.                  "passport":
  15.                  {
  16.                      "img_url": "https://sucimg.itc.cn/avatarimg/296670289_1444181118790",
  17.                      "user_id": 10115405,
  18.                      "nickname": "鱼可以飞么"
  19.                  },
  20.                  "user_id": 10115405,
  21.                  "from": "",
  22.                  "support_count": 0,
  23.                  "status": 1
  24.                 },
  25.                 "aaa":{
  26.                  "attachments": [],
  27.                  "comments": [],
  28.                  "create_time": 1556625475042,
  29.                  "ip": "",
  30.                  "comment_id": 150423360,
  31.                  "reply_count": 0,
  32.                  "content": "宇智波鼬的神器",
  33.                  "ip_location": "西藏自治区拉萨市",
  34.                  "reply_id": 0,
  35.                  "passport":
  36.                  {
  37.                      "img_url": "https://edc3f09a7c333.cdn.sohucs.com/s_mini/pic/2018/04/02/1.png",
  38.                      "user_id": 10115964,
  39.                      "nickname": "想个名咋这么难"
  40.                  },
  41.                  "user_id": 10115964,
  42.                  "from": "",
  43.                  "support_count": 0,
  44.                  "status": 1
  45.                  } }
  46.   newlst=[]
  47.   for x in jszd:
  48.      for key in jszd[x].keys():
  49.        tmp={}
  50.        if key=="passport":
  51.            tmp["user_id"]=jszd[x][key]["user_id"]
  52.            tmp["nick_name"]=jszd[x][key]["nickname"]
  53.            newlst.append(tmp)
  54.   print(newlst)        
复制代码


e:\>python ex16.py
[{'user_id': 10115405, 'nick_name': '鱼可以飞么'}, {'user_id': 10115964, 'nick_name': '想个名咋这么难'}]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-2 06:46:12 From FishC Mobile | 显示全部楼层
原始数据结构好像缺少点东西,第一层字典里的“  "comments": [  ”没有对应的“  ]  ”。
不过楼上貌似给你人工消除该问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-2 09:08:55 | 显示全部楼层
已经再这个帖子里回复了 https://fishc.com.cn/thread-134989-1-1.html

鉴于主楼得帖子内容不完整,所以此贴3楼代码可以忽视了,完整代码见https://fishc.com.cn/thread-134989-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-11 07:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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