亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python?接口返回的json字符串實(shí)例

 更新時(shí)間:2022年07月13日 14:12:57   作者:zhaoxian  
下面小編就為大家分享一篇python?接口返回的json字符串實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

如下所示:

JSON 函數(shù)
使用 JSON 函數(shù)需要導(dǎo)入 json 庫(kù):import json。

函數(shù)	描述
json.dumps	將 Python 對(duì)象編碼成 JSON 字符串
json.loads	將已編碼的 JSON 字符串解碼為 Python 對(duì)象


#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
import urllib2 
import urllib 
import cookielib
import json
 
def login(): 
 
  data={'username': '015208@xxxxx.com', 'password': '1234567'}
  post_data=urllib.urlencode(data) #將post消息化成可以讓服務(wù)器編碼的方式 
  cj=cookielib.CookieJar() #獲取cookiejar實(shí)例 
  opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
  #自己設(shè)置User-Agent(可用于偽造獲取,防止某些網(wǎng)站防ip注入) 
  headers = {} 
  website = "http://127.0.0.1:8000/api2/auth-token/"
  req=urllib2.Request(website,post_data,headers) 
  content=opener.open(req) 
  s= content.read() #linux下沒(méi)有g(shù)bk編碼,只有utf-8編碼
  print s
  print type(s)
  text = json.loads(s)
  print type(text)
  print text['token']
 
if __name__ == '__main__': 
 login() 

C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/untitled/a1.py
{"token": "2c73f3885ac90ee462daea49f1890730f567fbfe"}
<type 'str'>
<type 'dict'>
2c73f3885ac90ee462daea49f1890730f567fbfe

Process finished with exit code 0

以上這篇python 接口返回的json字符串實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論