首页 > 编程语言 >Python json基本使用json.dumps() 和json.loads()

Python json基本使用json.dumps() 和json.loads()

时间:2023-04-12 13:37:21浏览次数:37  
标签:dict1 Python dumps json print loads

Python中 json的基本使用 json.dumps() 和json.loads()

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它是JavaScript的子集,易于人阅读和编写。

Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式,现在也常用于http请求中。

Python中可用json模块来进行json字符串和Python数据类型的互相转换。

1. Python类型转为json字符串

可以使用 json.dumps() 方法将Python类型转换为 JSON 字符串。

import json
list1 = ['a', 'b', 123] str1 = '你好' dict1 = { "name": "张三", "info": {"family": ["father", "mother"], "age": 18}, "score": 87.5, "skill": "eat" } print(json.dumps(list1)) # ["a", "b", 123] print(json.dumps(str1)) # "\u4f60\u597d" # ensure_ascii参数表示是否将字符转为ASCII表示,False表示否 print(json.dumps(dict1, ensure_ascii=False)) # {"name": "张三", "info": {"family": ["father", "mother"], "age": 18}, "score": 87.5, "skill": "eat"} print(type(json.dumps(list1))) # <class 'str'>

 2.json字符串转为Python类型

可以使用json.loads() 方法将json字符串转换为Python类型。

j_dict = '{"name": "张三", "info": {"family": ["father", "mother"], "age": 18}, "score": 87.5, "skill": "eat"}'
dict1 = json.loads(j_dict)
print(dict1)
print(type(dict1))

 

3.将request请求得到的响应对象的text转为字典

要先确认response的text是json字符串。

import requests, json

url = 'http://www.xinfadi.com.cn/getPriceData.html'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
}

resp = requests.get(url, headers=headers)
print(resp.text)
print(json.loads(resp.text)['list'][0]['prodName'])
# 大白菜

 

标签:dict1,Python,dumps,json,print,loads
From: https://www.cnblogs.com/soooos/p/17309444.html

相关文章

  • 设置jupyter notebook的python environment
    (base)$condainstallnb_conda_kernels(base)$condacreate--namenew-env(base)$condaactivatenew-env(new-env)$condainstallipykernel(base)$condadeactivate(base)$jupyternotebookclickkernel,selectchangekernel,youwillseeyourenviro......
  • python爬虫(四):文本、图片、视频爬取实例
    上篇讲了常用的python爬虫工具,可以快速支撑我们数据的爬取--解析--分析,这里将拆解几个爬虫程序实例进行学习,实例来自于https://cuijiahua.com/blog/2020/04/spider-6.html的系列教程或者其他小爬虫;一、文本图表数据抓取(编程语言排名)#!/usr/bin/envpython#coding:utf-8importr......
  • python中的pandas小试
    在实习的项目中,得到宽表后的后续工作是用R语言写的,包括数据探索,数据清洗,建模,模型分析。因为之前用过python,写过简单爬虫,就想着自己试着将R语言的脚本写成python,或许对于未来有帮组、然而,在pyhon3.5连接teradata的问题上一直搞不通、、、所以,只能先学一下pandas之类的基础了,本来想法......
  • Python script get date and time All In One
    PythonscriptgetdateandtimeAllInOnePythonshellscriptprintcurrentdatetimetologfile#✅......
  • python - html转pdf
    1.安装pdfkitpip3installpdfkit2.安装wkhtmltopdf下载wkhtmltopdf安装包https://wkhtmltopdf.org/downloads.html安装后在系统Path添加wkhtmltopdf的bin路径3.简单使用将本地html转pdfimportpdfkit#如果html里引用了外部的文件,则需要添加以下参数wkhtmltopdf_o......
  • windows环境下python3安装Crypto
    pycrypto、pycrytodome和crypto是一个东西,crypto在python上面的名字是pycrypto,它是一个第三方库,但是已经停止更新三年了,所以不建议安装这个库;这个时候pycryptodome就来了,它是pycrypto的延伸版本,用法和pycrypto是一模一样的;所以,我现在告诉大家一种解决方法--直接安装:pipinstallpy......
  • Python中tqdm模块进度条
    tqdm模块简单介绍tqdm是一个可以显示Python进度条的模块,可通过pip在终端安装pipinstalltqdmtqdm.tqdmfromtqdmimporttqdmforiintqdm(range(1,5)):print(i)或传入字符串list:fromtqdmimporttqdmforiintqdm(["a","b","c"]):print(i)tqdm......
  • Python爬虫之循环爬取多个网页
    之前的文中介绍了如何获取给定网址的网页信息,并解析其中的内容。本篇将更进一步,根据给定网址获取并解析给定网址及其相关联网址中的内容。要实现这些功能,我们需要解决以下问题:1、如何持续不断的获取url,并读取相关内容。2、如何判断网址是否已经读取过。文中用到的代码均已上传......
  • linux安装两个python版本
    1.下载python3安装包wgethttps://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz2.解压python的tgz压缩包文件tar-xzvfPython-3.7.2.tgz3.进入解压的文件cdPython-3.7.24.在python文件路径下编译pythonprefix=/usr/local/python37,指定python安装路径,这个路径......
  • 全网最详细中英文ChatGPT-GPT-4示例文档-场景问题智能生成从0到1快速入门——官网推荐
    目录Introduce简介setting设置Prompt提示Sampleresponse回复样本APIrequest接口请求python接口请求示例node.js接口请求示例curl命令示例json格式示例其它资料下载ChatGPT是目前最先进的AI聊天机器人,它能够理解图片和文字,生成流畅和有趣的回答。如果你想跟上AI时代的潮流......