在服务器和客户端的数据交互的时候,要找到一种数据格式,服务端好处理,客户端也好处理,这种数据格式应该是一种统一的标准,不管在哪里端处理起来都是统一的,现在这种数据格式非常的多,比如最早的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. Cannot read property 'XXXX' of null/undifined

    这个问题可能的原因有很多 1.如果你的js直接写在自执行函数或者head标签内的script里面,那么可以检查一下你的代码有没有用到页面里的节点,因为这样写的代码在页面加载完成之前就会开始执行,如果有 ...

  2. 关于mysql一边查一边更新

    update test_table set user_id = 112 where id in (select id from ( select id from test_table where nu ...

  3. 使用tensorflow的retrain.py训练图片分类器

    参考 https://hackernoon.com/creating-insanely-fast-image-classifiers-with-mobilenet-in-tensorflow-f030 ...

  4. A brief introduction to complex analysis

    \(\underline{Def:}\)A func \(U(\subset \mathbb{C}) \stackrel{f}\longrightarrow \mathbb{C}\)is (compl ...

  5. 高精度处理斐波那契序列(C语言)

    #include<stdio.h> #include<string.h> //memset,strcpy,strlen函数头文件 int main(void) { ];//用来 ...

  6. C# 接口练习

    #define debug using System; using System.Collections; namespace ConsoleApp1 { interface IAnimal { in ...

  7. EF 执行存储过程

  8. window 彻底删除mysql

    1. 运行regedit.exe,找到HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\MySQL 文件夹,将其删除: 2. 找到HKEY_LOCAL_ ...

  9. POJ3276 Face The Right Way 开关问题

    ①每个K从最左边进行考虑 ②f[i]=[i,i+k-1]是否进行反转:1代表是,0代表否 ③∑ (i)(i=i+1-K+1) f[j]=∑ (i-1)(i=i-K+1) f[j]+f[i]-f[i-K ...

  10. Amsterdam Distance

    题目描述 Your friend from Manhattan is visiting you in Amsterdam. Because she can only stay for a short ...