Jwh199588
2019-01-03 21:09 阅读 1.7k

python中使用json解析字符串出现问题

在解析地址

https://api-zero.livere.com/v1/comments/list?callback=jQuery11240792331041974006_1546517344998&limit=10&repSeq=4272904&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1546517345000

生成的json数据的时候,发现解析错误,在查看过内容后发现json数据中有很多不需要的数据,怎么去除这些数据,让**json.loads(json_data)**能够解析成功

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • qq_24499417 qq_24499417 2019-01-03 21:18
    import  requests
    import json
    def get_response():
        url=r'https://api-zero.livere.com/v1/comments/list?limit=10&repSeq=4272904&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1546517345000'
        r=requests.get(url)
        dic=json.loads(r.text)
        return dic
    if __name__=='__main__':
        js=get_response()
        print(js['resultCode'])
    
    点赞 评论 复制链接分享
  • eye_water eye_water 2019-01-03 22:07

    找出第一个"{"字符出现的下标,利用切片获取想要的字符即可(可以使用正则表达式,不过我不是很熟悉)

    import json
    import requests
    
    r = requests.get('https://api-zero.livere.com/v1/comments/list?callback=jQuery11240792331041974006_1546517344998&limit=10&repSeq=4272904&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1546517345000')
    for i in range(len(r.text)):
        if r.text[i] == '{':
            index = i
            break
    text = r.text[index:-2]
    json_text = json.loads(text)
    print(json_text['results']['parents'][0])
    

    示例输出

    {'replySeq': 36631816, 'name': '用户6190237002', 'memberId': '6190237002', 'memberIcon': 'http://tvax3.sinaimg.cn/default/images/default_avatar_male_50.gif', 'memberUrl': 'https://weibo.com/u/6190237002', 'memberDomain': 'weibo_sina', 'good': 0, 'bad': 0, 'police': 0, 'parentSeq': 36631816, 'directSeq': 0, 'shortUrl': None, 'title': 'Hello world!', 'site': 'http://www.santostang.com/2018/07/04/hello-world/', 'email': None, 'ipAddress': '120.236.177.107', 'isMobile': '0', 'agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36', 'septSns': 'weibo_sina', 'targetService': None, 'targetUserName': None, 'info1': None, 'info2': None, 'info3': None, 'image1': None, 'image2': None, 'image3': None, 'link1': None, 'link2': None, 'link3': None, 'isSecret': 0, 'isModified': 0, 'confirm': 0, 'subCount': 0, 'regdate': '2018-12-27T15:10:40.000Z', 'deletedDate': None, 'file1': None, 'file2': None, 'file3': None, 'additionalSeq': 0, 'content': '我来评论一下', 'quotationSeq': None, 'quotationContent': None, 'consumerSeq': 1020, 'livereSeq': 28583, 'repSeq': 4272904, 'memberGroupSeq': 29677750, 'memberSeq': 30197150, 'status': 0, 'repGroupSeq': 0, 'adminSeq': 25413747, 'deleteReason': None, 'sticker': 0, 'version': None}
    
    点赞 评论 复制链接分享

相关推荐