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

环境配置

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

<span class="pln">$ tar xvfz demjson</span><span class="pun">-</span><span class="lit">1.6</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz
$ cd demjson</span><span class="pun">-</span><span class="lit">1.6</span><span class="pln">
$ python setup</span><span class="pun">.</span><span class="pln">py install</span>

JSON 函数

函数 描述
encode 将 Python 对象编码成 JSON 字符串
decode 将已编码的 JSON 字符串解码为 Python 对象

encode

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

语法

<span class="pln">demjson</span><span class="pun">.</span><span class="pln">encode</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> obj</span><span class="pun">,</span><span class="pln"> nest_level</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span>

实例

以下实例将数组编码为 JSON 格式数据:

<span class="com">#!/usr/bin/python</span>
<span class="kwd">import</span><span class="pln"> demjson

data </span><span class="pun">=</span> <span class="pun">[</span> <span class="pun">{</span> <span class="str">'a'</span> <span class="pun">:</span> <span class="lit">1</span><span class="pun">,</span> <span class="str">'b'</span> <span class="pun">:</span> <span class="lit">2</span><span class="pun">,</span> <span class="str">'c'</span> <span class="pun">:</span> <span class="lit">3</span><span class="pun">,</span> <span class="str">'d'</span> <span class="pun">:</span> <span class="lit">4</span><span class="pun">,</span> <span class="str">'e'</span> <span class="pun">:</span> <span class="lit">5</span> <span class="pun">}</span> <span class="pun">]</span><span class="pln">

json </span><span class="pun">=</span><span class="pln"> demjson</span><span class="pun">.</span><span class="pln">encode</span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span>
<span class="kwd">print</span><span class="pln"> json</span>

以上代码执行结果为:

<span class="pun">[{</span><span class="str">"a"</span><span class="pun">:</span><span class="lit">1</span><span class="pun">,</span><span class="str">"b"</span><span class="pun">:</span><span class="lit">2</span><span class="pun">,</span><span class="str">"c"</span><span class="pun">:</span><span class="lit">3</span><span class="pun">,</span><span class="str">"d"</span><span class="pun">:</span><span class="lit">4</span><span class="pun">,</span><span class="str">"e"</span><span class="pun">:</span><span class="lit">5</span><span class="pun">}]</span>

decode

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

语法

<span class="pln">demjson</span><span class="pun">.</span><span class="pln">decode</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> txt</span><span class="pun">)</span>

实例

以下实例展示了Python 如何解码 JSON 对象:

<span class="com">#!/usr/bin/python</span>
<span class="kwd">import</span><span class="pln"> demjson

json </span><span class="pun">=</span> <span class="str">'{"a":1,"b":2,"c":3,"d":4,"e":5}'</span><span class="pun">;</span><span class="pln">

text </span><span class="pun">=</span><span class="pln"> demjson</span><span class="pun">.</span><span class="pln">decode</span><span class="pun">(</span><span class="pln">json</span><span class="pun">)</span>
<span class="kwd">print</span><span class="pln">  text</span>

以上代码执行结果为:

 

Comments are closed.

Post Navigation