在服务器和客户端的数据交互的时候,要找到一种数据格式,服务端好处理,客户端也好处理,这种数据格式应该是一种统一的标准,不管在哪里端处理起来都是统一的,现在这种数据格式非常的多,比如最早的xml,再后来较为流行的json。

JSON是什么

JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。

JSON 比 XML 更小、更快,更易解析。

json格式如下:

{
"sites": [
{
"name": "阿猫学编程",
"url": "www.bugingcode.com"
},
{
"name": "博客园",
"url": "www.cnblogs.com"
},
{
"name": "cndn",
"url": "www.csdn.com"
}
]
}

python中如何解析json

既然json是一个通用的数据交换方式,那么python中如何解析json呢?

在python中标准库就能对json字符串进行解析,同时把python的数据结构转换为json格式字符串。

把字符串json解析为python的数据结构:

#!/usr/bin/python
#coding=utf-8 """
start python 项目
""" import json if __name__ == '__main__':
jsonstr="""{
"sites": [
{
"name": "阿猫学编程",
"url": "www.bugingcode.com"
},
{
"name": "博客园",
"url": "www.cnblogs.com"
},
{
"name": "cndn",
"url": "www.csdn.com"
}
]
}""" print jsonstr sites = json.loads(jsonstr)
print sites
print sites['sites']
for site in sites['sites']:
print site['name'],site['url']

把python中的数据结构转换为json格式:

#!/usr/bin/python
#coding=utf-8 """
start python 项目
""" import json if __name__ == '__main__': sites = {'sites':[{"name": "阿猫学编程","url": "www.bugingcode.com"},{"name": "博客园","url": "www.cnblogs.com"},{"name": "csdn","url": "www.csdn.com"}]} jsonstr = json.dumps(sites) print jsonstr

更多教程:阿猫学编程

在python中使用json的更多相关文章

  1. python中的 json 模块使用

    (1)python 中生成 json 字符串: import json data = dict(ret=0, msg="Welcome, Login success!") json ...

  2. 在 Python 中使用 JSON

    在 Python 中使用 JSON 本教程将会教我们如何使用 Python 编程语言编码和解码 JSON.让我们先来准备环境以便针对 JSON 进行 Python 编程. 环境 在我们使用 Pytho ...

  3. 【python】python中的json、字典dict

    定义 python中,json和dict非常类似,都是key-value的形式,而且json.dict也可以非常方便的通过dumps.loads互转.既然都是key-value格式,为啥还需要进行格式 ...

  4. Python中的json操作

    Python中的json操作 标签(空格分隔): python 编码 json 字符串前缀问题 字符串前缀可以有r,u r:表示原始(raw)字符串,比如'\n'不会被转义.常用于正则. u:表示un ...

  5. python中读取json文件报错,TypeError:the Json object must be str, bytes or bytearray,not ‘TextIOWrapper’

    利用python中的json读取json文件时,因为错误使用了相应的方法导致报错:TypeError:the Json object must be str, bytes or bytearray,n ...

  6. python中的json的基本使用方法

    在python中使用json的时候,主要也就是使用json模块,json是以一种良好的格式来进行数据的交互,从而在很多时候,可以使用json数据格式作为程序之间的接口, #!/usr/bin/env ...

  7. Python中的json学习

    p.p1 { margin: 0; font: 14px ".PingFang SC"; color: rgba(53, 53, 53, 1) } p.p2 { margin: 0 ...

  8. python 中的json解析库

    当一个json 数据很大的时候.load起来是很耗时的.python中常见的json解析库有cjson,simplesjson,json, 初步比较了一下, 对于loads来讲 simplejson ...

  9. 在Python中进行JSON转化

    序列化,指的是把内存中的变量(如类的实例)变成可存储或可传输的过程. JSON(JavaScript Object Notation, JavaScript对象表示)是网络传输中经常使用的一种数据形式 ...

  10. Python中模块json与pickle的功能介绍

    json & pickle & shelve 1. json的序列化与反序列化 json的使用需要导入该模块,一般使用import json即可. json的序列化 方法1:json. ...

随机推荐

  1. \_\_getattribute\_\_

    __getattribute__ 一.__getattr__ 不存在的属性访问,触发__getattr__ class Foo: def __init__(self, x): self.x = x d ...

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...

  3. oracle 导入导出参数

  4. TPO5-1 Minerals and plants

    Only recently have investigators considered using these plants to clean up soil and waste sites that ...

  5. yum pip

    方式1(yum安装):1.首先安装epel扩展源:[root@localhost ~]#  yum -y install epel-release如果没有安装epel扩展源而直接安装python-pi ...

  6. 初识OpenGl

    函数命名规则 OpenGl函数都遵循一个命名约定:<库前缀> <根命令> <可选参数个数> <可选参数类型> 如:glColor3f() ,gl:核心库 ...

  7. Dangal 观影感受,(摘录)

    ===================================================================================== 引用: https://ww ...

  8. 基于 maven 的ssm 框架搭建

    1.新建一个 maven 工程, war 包 2.引入 pom 文件(springmvc+spring+mybatis) 3.引入配置文件 4.引入页面,编写 contorller 层测试 5.编写查 ...

  9. 通俗易懂JSONP讲解

    原文地址:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html JSON的格式或者叫规则: JSON能够 ...

  10. php 二维码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...