python介面測試之json模組的使用

來源:互聯網
上載者:User

標籤:www.   print   com   sharp   序列   類型   python介面   IV   方法   

  1. json.dumps()
  2. json.loads()
  3. json.dump
  4. json.load

一、json.dumps()

將python字典類型轉換成json對象

import json# Python 字典類型轉換為 JSON 對象data = {    ‘no‘ : 1,    ‘name‘ : ‘Runoob‘,    ‘url‘ : ‘http://www.runoob.com‘}json_str = json.dumps(data)print ("Python 未經處理資料:", repr(data))print ("JSON 對象:", json_str)

 

二、json.loads()

將json對象轉換成python字典

# 將 JSON 對象轉換為 Python 字典data2 = json.loads(json_str)print ("data2[‘name‘]: ", data2[‘name‘])print ("data2[‘url‘]: ", data2[‘url‘])

 

三、json.dump

把序列化後的字串寫到檔案中

四、json.load

從檔案中讀取檔案內容

#然後來介紹dump方法# import json# d1 = {‘name‘:‘foot‘}#這一步就會把d1做序列化處理後的字串寫到db這個檔案中# json.dump(d1,open(‘db‘,‘w‘))# d1 = json.load(open(‘db‘,‘r‘))# print(d1,type(d1))# {‘name‘: ‘foot‘} <class ‘dict‘>

  

 

python介面測試之json模組的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.