美文网首页
Python中的json.dumps()和json.loads(

Python中的json.dumps()和json.loads(

作者: Gakki0725 | 来源:发表于2021-07-05 10:56 被阅读0次
作者:Gakki

JSON 是一种轻量级得到数据交换格式,易于人阅读和编写。

01 函数的描述

函数 描述
json.load 从文件中读取json字符串
json.dump 将json格式字符串写到文件中
json.loads 将已编码的 JSON 字符串解码为 Python 对象
json.dumps 将 Python 对象编码成 JSON 字符串

02 实例

  • json.load():从文件中读取json字符串
import json
with open('text.json','r',encoding='utf-8') as f :
    print(json.load(f))

输出结果:
{'name': 'test', 'age': '20'}

  • json.dump():将json格式字符串写到文件中
import json
content = '{ "name": "test", "age": "20" }'
with open('text2.json','w',encoding='utf-8') as f :
    json.dump(content,f)

输出结果:
"{ \"name\": \"test\", \"age\": \"20\" }"

  • json.loads():将已编码的 JSON 字符串解码为 Python 对象
import json
content = '{"name":"test","age":"20"}'
print(type(json.loads(content)))
print(json.loads(content))

输出结果:

<class 'dict'>
{'name': 'test', 'age': '20'} 
  • json.dumps():将 Python 对象编码成 JSON 字符串
import json
content = '{"name":"test","age":"20"}'
print(type(json.dumps(content)))
print(json.dumps(content))

输出结果:

<class 'str'>
"{\"name\":\"test\",\"age\":\"20\"}"

相关文章

  • json对象和json字符串之间的转化

    关键词:json python中,json与对象的转换:json.dumps()/json.loads(); js...

  • python中json处理

    python中json文件处理涉及的四个函数json.loads()、json.dumps()、json.load...

  • json库

    python的json模块提供了编码和解码json的方法,json.dumps()和json.loads(),其中...

  • Python网络请求和解析json数据

    处理json字符串 Python的json模块提供了两个函数 json.dumps()和 json.loads()...

  • Json

    json.loads() :将字符串转换为Python数据类型json.dumps() :将Python数据类型转...

  • json、多任务

    json json.loads() : 将json字符串转化为Python数据类型json.dumps() : 将...

  • json

    json.dumps 将 Python 对象编码成 JSON 字符串 json.loads 将已编码的 JSON ...

  • python json模块

    json.dumps 将 Python 对象编码成 JSON 字符串 json.loads 将已编码的 JS...

  • python之JSON易混的两个函数

    python中经常会使用json模块,最常用的两个方法就是json.loads和json.dumps 要将包含 J...

  • Python中的json.dumps()和json.loads(

    作者:Gakki JSON 是一种轻量级得到数据交换格式,易于人阅读和编写。 01 函数的描述 函数描述json....

网友评论

      本文标题:Python中的json.dumps()和json.loads(

      本文链接:https://www.haomeiwen.com/subject/oqjmultx.html