Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the ways to deal with JSON data in Python

Shulou Source: shulou.com Published: 2022-06-01 20:24:57 09月20日 Update

Editor to share with you what are the JSON data processing methods in Python. I hope you will get something after reading this article. Let's discuss it together.

Json.loads ()

Json.loads is used to decode JSON data and decode Json format strings into Python objects.

Import jsonarr = [1,2,3,4] print (json.loads (str (arr) dic ='{"name": "xiaoming", "age": 18} 'print (json.loads (dic)) # [1,2,3,4] # {' name': 'xiaoming',' age': 18} json.dumps ()

Convert an Python object encoding to a Json string

Import jsonarr = [1,2,3,4] print (json.dumps (arr)) dic = {"name": "xiaoming", "age": 18} print (json.dumps (dic)) json.dump ()

Write to the file after serializing the Python built-in type into a json object, ensure_ascii is more critical, and the True representative is displayed as an encoded form, which is generally very difficult to use in Chinese, so it is recommended to turn it off.

Import jsondic = {"name": "xiaohong", "age": 18} json.dump (dic, open ('json.txt', 'w'), ensure_ascii=False) json.load ()

Read string elements in json form in the file and convert them to python type

Import jsondic = {"name": "xiaohong", "age": 18} json.dump (dic, open ('json.txt', 'w'), ensure_ascii=False) content = json.load (open (' json.txt')) print (type (content), content) after reading this article, I believe you have a certain understanding of "what are the methods of JSON data processing in Python". If you want to know more about it, please follow the industry information channel. Thank you for reading!

Tags: Data character string object data processing method processing form file article type coding bad representative element key done sequence suggestion more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Huawei NVidia Shulou Tech Info