pythonjson安装_Python JSON - Python 基础教程 - 自强学堂

Python JSON

本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象。

Python 2.7 自带 JSON 模块【官方文档】

1. 从python原始类型向json类型的转换过程,具体的转换如下:import json

json.dump(obj, fp, skipkeys=False,ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False, **kw)

json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding="utf-8",default=None, sort_keys=False, **kw)

python                        json

------------------------------------------

dict                           object

list,tuple                   array

str,unicode               string

int,long,float             number

True                          true

False                        false

None                        null>>> import json

>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])

'["foo", {"bar": ["baz", null, 1.0, 2]}]'

>>> f = open('demo.txt','w')

>>> json.dump(range(100), f)

# 打开 demo.txt 可以看到

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

2. 从 json 类型向 python 的转换过程,具体的转换如下:import json

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

json                           python

-----------------------------------------

object                         dict

array                           list

string                          str

number(int)                int

number(real)              float

true                            True

false                           False

null                             None>>> import json

>>> json.loads('{"a": 1, "b": 2, "c": 3}') # string to python objects

{u'a': 1, u'c': 3, u'b': 2}

# 1.txt 中的内容

[{"a": [1, 2, 3, 4, 5]}, {"b": [1, 2, 3]}, {"c": ["English", "Chinese"]}]

>>> f = open('1.txt')

>>> json.load(f)

[{u'a': [1, 2, 3, 4, 5]}, {u'b': [1, 2, 3]}, {u'c': [u'English', u'Chinese']}]

旧版本 Python 环境配置(Python中自带json模块的用户不用看)

在使用 Python 编码或解码 JSON 数据前,我们需要先安装 JSON 模块。本教程我们会下载 Demjson 并安装:$tar xvfz demjson-1.6.tar.gz

$cd demjson-1.6

$python setup.py install

JSON 函数函数描述

encode将 Python 对象编码成 JSON 字符串

decode将已编码的 ‍‍‍‍JSON‍‍‍‍ 字符串解码为 Python 对象

encode

Python encode() 函数用于将 Python 对象编码成 JSON 字符串。

语法demjson.encode(self, obj, nest_level=0)

实例

以下实例将数组编码为 JSON 格式数据:#!/usr/bin/python

import demjson

data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]

json = demjson.encode(data)

print json

以上代码执行结果为:[{"a":1,"b":2,"c":3,"d":4,"e":5}]

decode

Python 可以使用 demjson.decode() 函数解码 JSON 数据。该函数返回 Python 字段的数据类型。

语法demjson.decode(self, txt)

实例

以下实例展示了Python 如何解码 JSON 对象:#!/usr/bin/python

import demjson

json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

text = demjson.decode(json)

print  text

以上代码执行结果为:{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值