python json module (15)

   

 

JSON stands for JavaScript Object Notation, namely JavaScript Object Notation, which is a lightweight, cross-platform, cross-language data exchange format, the design intent is to put everything represented by the string design, so convenient transmission of information on the Internet, but also to facilitate people to read.

 

 

    JSON widely used mainly in C-like programming language. These languages include C, C ++, C #, the Java, JavaScript, Perl, Python  and so on. JSON provides the ability to perform data exchange between multiple languages, thus, is an ideal JSON data exchange format.

json

A .python json module Introduction

    json module provides support for the JSON, it includes both the recovery string into Python JSON object functions, also are provided to convert an object into Python JSON string functions.

    json.dumps () - json dumps function module in the encoded data is formed json data format;

    json.loads () - function loads data decoding sucked json format, converted to Python dictionary;

python-json
 
 

Two .json conversion to python

 

json turn python

 

import json
 
data_str = '{"url": "www.shuopython.com", "name": "猿说python", "contents": "python教程"}'
data_dict = json.loads(data_str)
print(data_dict)
print(type(data_dict))

 

    输出结果:

    注意:data_str 是json字符串,并非字典也并非字符串str

 

三.python转换到json

 

python turn json

 

    在使用dump/dumps时可能会使用到以下参数:

    skipkey:默认为False,当dict对象里的数据不是Python的基本数据类型;(str,unicode,int,long,float,bool,None)时,当skipkey为False,就会报错,如果skipkey为True,则可以跳过这类key;

    indent:如果填0或者不填,则按照一行进行打印,否则按照indent的数值显示前面的空格(正整数形式);

    separators:分隔符,默认为“(‘,’,’:’)”,它表示key之间用“,”隔开,key和value之间用“:”隔开;

    encoding:编码格式,默认值是UTF-8;

    sort_keys:对key、value进行排序,默认值是False,即不排序;

    ensure_ascii:默认为True,如果dict对象里含有none-ASCII的字符,则显示\uXX的格式,如果为False,则能正常显示出来(解决中文乱码问题);

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_json.py
@Time:2019/11/08 21:25
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
 
 
import json
 
data_dict = {"url": "www.shuopython.com", "name": "猿说python", "contents": "python教程"}
 
# ensure_ascii 默认为True,如果有中文,显示\uXX的格式;设置为False可解决中文显示不正常问题
json1 = json.dumps(data_dict, ensure_ascii=False,indent=4) # indent 字符缩进,输出更加美观
print(json1)
print(type(json1))

 

 

输出结果:

{
    "url": "www.shuopython.com",
    "name": "猿说python",
    "contents": "python教程"
}
<class 'str'>

 

 

 

四.关于json.dump()和 json.load()

    相对于上面所讲的dumps和loads来说,dump和load函数的功能类似,只不过前者是用来处理字符串类型的,而后者是用于处理文件类型的,如下所示:

import json
 
 
data = {'str3':'xyz','str2' :'efgh','str1':'abcd'}
with open(' data. txt', '', encoding='utf-8') as f:
    json.dump(data, f, indent=4)   # f. write (json.dumps (data, indent=4))  #和上面的效果一样
 
with open(' data. txt', ' R & lt ' ) AS F: 
    D2 = the json.load (F)   # . Json.loads D2 = (F Read ()) # same effect as above

 

 

 

you may also like:

    1.python time module

    2.python sys module

    3.python variable length parameters * argc, ** kargcs

    4.python anonymous function lambda

    5.python recursive function

    6.python Exception Handling

 

    Reproduced please specify: ape say Python  »  Python module json

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/12081280.html