首页 > 编程 > Python > 正文

Python JSON

2019-11-06 06:06:39
字体:
来源:转载
供稿:网友

1 JSON

JSON (javaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the Javascript PRogramming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as anobject, record, struct, dictionary, hash table, keyed list, or associative array.An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

In JSON, they take on these forms:

An object is an unordered set of name/value pairs. An object begins with { (left brace)and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

value can be a string in double quotes, or a number, or true or false or null, or anobject or an array. These structures can be nested.

string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

参考:http://json.org/

JSON 语法规则

JSON 语法是 JavaScript 对象表示法语法的子集。

数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组

JSON 名称/值对

JSON 数据的书写格式是:名称/值对。

名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:

"firstName" : "John"

这很容易理解,等价于这条 JavaScript 语句:

firstName = "John"

JSON 值

JSON 值可以是:

数字(整数或浮点数)字符串(在双引号中)逻辑值(true 或 false)数组(在方括号中)对象(在花括号中)null

JSON 对象

JSON 对象在花括号中书写:

对象可以包含多个名称/值对:

{ "firstName":"John" , "lastName":"Doe" }

这一点也容易理解,与这条 JavaScript 语句等价:

firstName = "John"lastName = "Doe"

JSON 数组

JSON 数组在方括号中书写:

数组可包含多个对象:

{"employees": [{ "firstName":"John" , "lastName":"Doe" },{ "firstName":"Anna" , "lastName":"Smith" },{ "firstName":"Peter" , "lastName":"Jones" }]}

在上面的例子中,对象 "employees" 是包含三个对象的数组。每个对象代表一条关于某人(有姓和名)的记录。

JSON 文件

JSON 文件的文件类型是 ".json"JSON 文本的 MIME 类型是 "application/json"

参考:http://www.runoob.com/json/json-tutorial.html

2 Python JSON

两个主要的函数是 json.dumps() 和 json.loads()

3 实例

3.1 json.dumps

在做HTTP API开发时,通过REST Client下发POST请求,请求的Payload为:

{    "target_ip": "192.168.16.120",    "kwargs": {        "server_ip": "192.168.111.117"    }}HTTP请求的自定义Header是:

Accept	application/json	  Content-Type	application/json

当程序收到Payload后,打印结果与入库后的结果都是Unicode串:

{'target_ip': u'192.168.16.120', 'kwargs': {u'server_ip': u'192.168.111.111'}}

但是要在库中存非Unicode串,怎么做?

用json.dumps来转换字符串,转换后输出:

{"target_ip": "192.168.16.120", "kwargs": {"server_ip": "192.168.111.111"}用python执行:

>>> import json>>> data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]>>> json.dumps(data)'[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]'>>> data = {'target_ip': u'192.168.16.120', 'kwargs': {u'server_ip': u'192.168.111.111'}}>>> json.dumps(data)'{"target_ip": "192.168.16.120", "kwargs": {"server_ip": "192.168.111.111"}}'>>> 


上一篇:python面试题二

下一篇:python面试题集

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表