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 對象表示法語法的子集。
數據在名稱/值對中數據由逗號分隔花括號保存對象方括號保存數組JSON 數據的書寫格式是:名稱/值對。
名稱/值對包括字段名稱(在雙引號中),后面寫一個冒號,然后是值:
"firstName" : "John"這很容易理解,等價于這條 JavaScript 語句:
firstName = "John"JSON 值
JSON 值可以是:
數字(整數或浮點數)字符串(在雙引號中)邏輯值(true 或 false)數組(在方括號中)對象(在花括號中)nullJSON 對象
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"}}'>>>
新聞熱點
疑難解答