企业管理系统定制开发【python】json.dumps() 与 json.loads() 用法

文章目录

一、JSON介绍

JSON代表JavaScript对象符号。企业管理系统定制开发它是一种轻量级的数据交换格式,企业管理系统定制开发用于存储和交换数据。它是一种独立于语言的格式,非常容易理解,因为它本质上是自描述的。 python中有一个内置包,它支持JSON数据,称为json。 JSON中的数据表示为quoted-strings,由大括号{}之间的键值映射组成。通俗来说就是一种在接口中易于使用的数据处理模块,但是json不属于数据格式。

二、Python和Json数据类型的映射

JSONPython
objectdict
arraylist
stringstr
numberint
trueTrue
falseFalse
nullNone

三、json.load(s)与json.dump(s)区别

json.load:表示读取文件,返回python对象
json.dump:表示写入文件,文件为json字符串格式,无返回
json.dumps:将python中的字典类型转换为字符串类型,返回json字符串 [dict→str]
json.loads:将json字符串转换为字典类型,返回python对象 [str→dict]
load和dump处理的主要是 文件
loads和dumps处理的是 字符串

json.load()从json文件中读取数据
json.loads()将str类型的数据转换为dict类型
json.dumps()将dict类型的数据转成str
json.dump()将数据以json的数据类型写入文件中

四、测试

4.1 json.dumps()

import jsondata = {    'fruit':'apple',    'vegetable':'cabbage'}print(data,type(data))data = json.dumps(data)  # dict转jsonprint(data,type(data))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>{"fruit": "apple", "vegetable": "cabbage"} <class 'str'>
  • 1
  • 2

4.2 json.loads()

data = """{"fruit": "apple","vegetable": "cabbage"}"""# 一般此时data为request.text返回值print(data, type(data))data = json.loads(data)print(data, type(data))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

返回:

{"fruit": "apple","vegetable": "cabbage"} <class 'str'>{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
  • 1
  • 2
  • 3
  • 4
  • 5

4.3 json.dump()

1.写str
a.py中:

data = "wyt"with open('b.json', 'w') as f:    json.dump(data, f)with open('b.json','r',encoding='utf-8') as f :    f_str = json.load(f)    print(f_str,type(f_str))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

返回:

wyt <class 'str'>
  • 1

2.写dict
a.py中:

data = {    'fruit':'apple',    'vegetable':'cabbage'}with open('b.json', 'w') as f:    json.dump(data, f)with open('b.json','r',encoding='utf-8') as f :    f_str = json.load(f)    print(f_str,type(f_str))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
  • 1

4.4 json.load()

a.json中存在:

{"fruit": "apple","vegetable": "cabbage"}
  • 1
  • 2
  • 3
  • 4

a.py中:

with open('a.json','r',encoding='utf-8') as f :    f_str = json.load(f)    print(f_str,type(f_str))
  • 1
  • 2
  • 3

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
  • 1

五、报错分析

5.1 本地代码

data = '''{'fruit':'apple','vegetable':'cabbage'}'''data = json.loads(data)
  • 1
  • 2
  • 3
  • 4
  • 5

5.2 报错返回

json..JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

5.3 报错分析与解决

json内部要使用双引号。

data = """{"fruit": "apple","vegetable": "cabbage"}"""data = json.loads(data)print(data, type(data))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
  • 1
网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发