欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python接口返回的json字符串實例-創(chuàng)新互聯(lián)

如下所示:

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了淮安免費建站歡迎大家使用!
JSON 函數(shù)
使用 JSON 函數(shù)需要導入 json 庫:import json。

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


#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
import urllib2 
import urllib 
import cookielib
import json
 
def login(): 
 
  data={'username': '015208@zjtlcb.com', 'password': '1234567'}
  post_data=urllib.urlencode(data) #將post消息化成可以讓服務器編碼的方式 
  cj=cookielib.CookieJar() #獲取cookiejar實例 
  opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
  #自己設置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下沒有gbk編碼,只有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字符串實例-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://chinadenli.net/article6/diddog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷品牌網(wǎng)站建設外貿(mào)網(wǎng)站建設網(wǎng)頁設計公司營銷型網(wǎng)站建設網(wǎng)站導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司