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

The method of conversion between object and json realized by python3

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The method of converting objects implemented by python3 into json. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Conversion operation between object and json implemented by python3

1. Python has three main data types: dictionary, list, and tuple, which are represented by curly braces, square brackets and parentheses respectively. For example:

Dictionary: dictData= {'aqiaqizuo 12 thecontrolling breadcracy 34}.

List: listData= [1, 2, 3, 4]

Tuple: tupleData= (1, 2, 3, 4)

two。 When using json, you can import import json.

3. Python object to json string, the main function used: json.dumps (). For example: example 1:

#! / usr/bin/env python3

#-*-coding: utf-8-*-

Import json

""

Convert python object to json string

""

# construct a dictionary

Python2json = {}

# construct list

ListTest = [1pm 2pm 3]

Python2json ["listTest"] = listTest

Python2json ["strData"] = "Python object to json."

# convert to json string

JsonStr = json.dumps (python2json)

Print (jsonstr)

Results:

{"listTest": [1,2,3], "strData": "Python object to json."} example 2:

#! / usr/bin/env python3

#-*-coding: utf-8-*-

Import json

""

Convert python object to json string

""

# construct a dictionary

Python2json = {}

# construct dict

DictData = {'adept: 123,' baked: [3, 4, 5], 'censor: (4, 5, 5)}

Python2json ["dictTest"] = dictData

# convert to json string

JsonStr = json.dumps (python2json)

Print (jsonStr)

Results:

{"dictTest": {"a": 123b ": [3,4,5]," c ": [4,5,6]}} the main function used to convert 4.json to python object is: json.loads (). For example:

#! / usr/bin/env python3

#-*-coding: utf-8-*-

Import json

""

Json to python object

""

Str ='{"Data1": [1,2,3], "Data2": "Json to python object."}'

Json2python = json.loads (str)

Print (json2python)

Results:

The format of {'Data1': [1,2,3],' Data2': 'Json to python object.'} 5.json encoding is almost the same as python syntax, except that True will be mapped to true,False, false,None will be mapped to null, and tuples () will be mapped to list [].

This is the answer to the question about the conversion between python3 objects and json. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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: 0

*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