国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python JSON

2019-11-06 06:06:39
字體:
供稿:網(wǎng)友

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 語法規(guī)則

JSON 語法是 JavaScript 對(duì)象表示法語法的子集。

數(shù)據(jù)在名稱/值對(duì)中數(shù)據(jù)由逗號(hào)分隔花括號(hào)保存對(duì)象方括號(hào)保存數(shù)組

JSON 名稱/值對(duì)

JSON 數(shù)據(jù)的書寫格式是:名稱/值對(duì)。

名稱/值對(duì)包括字段名稱(在雙引號(hào)中),后面寫一個(gè)冒號(hào),然后是值:

"firstName" : "John"

這很容易理解,等價(jià)于這條 JavaScript 語句:

firstName = "John"

JSON 值

JSON 值可以是:

數(shù)字(整數(shù)或浮點(diǎn)數(shù))字符串(在雙引號(hào)中)邏輯值(true 或 false)數(shù)組(在方括號(hào)中)對(duì)象(在花括號(hào)中)null

JSON 對(duì)象

JSON 對(duì)象在花括號(hào)中書寫:

對(duì)象可以包含多個(gè)名稱/值對(duì):

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

這一點(diǎn)也容易理解,與這條 JavaScript 語句等價(jià):

firstName = "John"lastName = "Doe"

JSON 數(shù)組

JSON 數(shù)組在方括號(hào)中書寫:

數(shù)組可包含多個(gè)對(duì)象:

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

在上面的例子中,對(duì)象 "employees" 是包含三個(gè)對(duì)象的數(shù)組。每個(gè)對(duì)象代表一條關(guān)于某人(有姓和名)的記錄。

JSON 文件

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

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

2 Python JSON

兩個(gè)主要的函數(shù)是 json.dumps() 和 json.loads()

3 實(shí)例

3.1 json.dumps

在做HTTP API開發(fā)時(shí),通過REST Client下發(fā)POST請(qǐng)求,請(qǐng)求的Payload為:

{    "target_ip": "192.168.16.120",    "kwargs": {        "server_ip": "192.168.111.117"    }}HTTP請(qǐng)求的自定義Header是:

Accept	application/json	  Content-Type	application/json

當(dāng)程序收到Payload后,打印結(jié)果與入庫后的結(jié)果都是Unicode串:

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

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

用json.dumps來轉(zhuǎn)換字符串,轉(zhuǎn)換后輸出:

{"target_ip": "192.168.16.120", "kwargs": {"server_ip": "192.168.111.111"}用python執(zhí)行:

>>> 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面試題集

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南通市| 新泰市| 津市市| 灵武市| 古浪县| 夏邑县| 和林格尔县| 专栏| 闻喜县| 衢州市| 崇州市| 留坝县| 康平县| 政和县| 会昌县| 永修县| 永平县| 清水河县| 阿荣旗| 和政县| 延吉市| 安仁县| 台湾省| 福清市| 六枝特区| 沧源| 黄陵县| 温泉县| 祁连县| 梁平县| 当阳市| 红河县| 南平市| 彰化市| 新津县| 抚宁县| 瓮安县| 犍为县| 保靖县| 怀集县| 满洲里市|