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).
A 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.
A 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.
A 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 語法是 JavaScript 對(duì)象表示法語法的子集。
數(shù)據(jù)在名稱/值對(duì)中數(shù)據(jù)由逗號(hào)分隔花括號(hào)保存對(duì)象方括號(hào)保存數(shù)組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)中)nullJSON 對(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"}}'>>>
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注