Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Python json.dumps () function

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use the Python json.dumps () function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Json.dumps converts a Python data structure to JSON

Import jsondata = {'name':' myname', 'age': 100,100} json_str = json.dumps (data) some uses of the json library

The method uses json.dumps () to encode python objects into Json strings, json.loads () to decode Json strings into python objects, json.dump () to convert objects in python into json and store them in files, json.load () to convert the format of json in files into python object extraction.

The difference between json.dump () and json.dumps ()

Json.dumps () is a process of converting a python object into a json object, which generates a string.

Json.dump () is a file stream that converts python objects into json objects to generate a fp, which is related to the file.

Json parameter

Json.dumps (obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding= "utf-8", default=None, sort_keys=False, * * kw)

Obj: an object that is converted to json.

Sort_keys = True: tells the encoder to sort the output according to the dictionary (a to z). If it is a python object of dictionary type, sort the keywords by dictionary.

Indent: parameters are indented according to the data format and read more clearly.

Separators: is the meaning of the delimiter. The parameter means the delimiter between different dict items and the delimiter between key and value in the dict entry, removing the spaces that follow: and.

Import jsonx = {'name':' you guess', 'age':19,'city':' Sichuan'} # Encoding python in dumps into the json string y = json.dumps (x) print (y) I = json.dumps (XMAS = (',',':)) print (I) # output result {"name": "u4f60u731c", "age": 19, "city": "u56dbu5ddd"} {"name": "u4f60u731c", "age": 19, "city": "u56dbu5ddd"}

Skipkeys: the default value is False. If the data in the keys of dict is not the basic type of python (str,unicode,int,long,float,bool,None), if it is set to False, an error of TypeError will be reported. If it is set to True, this type of key will be skipped.

Ensure_ascii=True: the default output ASCLL code, if this should be False, you can output Chinese.

Check_circular: if check_circular is false, the circular reference check on the container type is skipped and the circular reference will result in an overflow error (or worse).

Allow_nan: if allow_nan is false, ValueError will serialize floating-point values that are out of range (nan, inf,-inf), strictly following the JSON specification, rather than using values like JavaScript (nan, Infinity,-Infinity).

Default:default (obj) is a function that should return a serializable version of obj or throw a type error. The default value only raises a type error.

The difference between dump and dumps in json

Json.dumps () is used to convert python objects into json strings and return the converted json strings.

Import json# converts python objects to json strings persons = [{'username': "zhaoji", "age": "18", "country": "China"}, {"username": "cyj", "age": "18" "country": "China"}] # call the dumps method to convert the python object json_str = json.dumps (persons) # print the data type of the converted json string print (type (json_str)) # print the converted json string print (json_str)

The output is

[{"username": "zhaoji", "age": "18", "country": "China"}, {"username": "cyj", "age": "18", "country": "China"}]

Process finished with exit code 0

Json.dump () is used to convert the python object to a string and write to the file

Import json# converts python objects to json strings persons = [{'username': "zhaoji", "age": "18", "country": "China"}, {"username": "cyj", "age": "18", "country": "China"}] with open (". / data/j1.json") W) as fp: json.dump (persons, fp) fp.close ()

Write the file as

[{"username": "zhaoji", "age": "18", "country": "China"}, {"username": "cyj", "age": "18", "country": "China"}]

That's all for the content of "how to use the Python json.dumps () function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report