python開發_json_一種輕量級的數據交換格式


以下是我做的對於python中json模塊的demo

運行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 

    JSON(JavaScript Object Notation)是一種輕量級的數據交換
    格式。易於人閱讀和編寫,同時也易於機器解析和生成。

    在python中,json模塊提供的dumps()方法可以對簡單的數據進行編碼:
    import json
    
    obj = [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy',(88, 42, 'hongten'), {'name' : 'hongten'}]
    encodedjson = json.dumps(obj)
    print(repr(obj))
    print(encodedjson)

    #輸出:
    #[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}]
    #[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}]

    objA = [True, False, None]
    encodedjsonA = json.dumps(objA)
    print(repr(objA))
    print(encodedjsonA)

    #輸出:
    #[True, False, None]
    #[true, false, null]

    在json的編碼過程中,會存在從python原始類型向json類型的轉換過程,具體的轉換
    如下:

        python      -->           json
        dict                      object
        list,tuple                array
        str,unicode               string
        int,long,float            number
        True                      true
        False                     false
        None                      null

    json轉換為python數據類型:
    import json
    testB = 'hongten'
    dump_test = json.dumps(testB)
    print(testB)
    print(dump_test)
    load_test = json.loads(dump_test)
    print(load_test)

    #輸出:
    #hongten
    #"hongten"
    #hongten
    
    而json轉換為python類型的時候,調用的是json.loads()方法,按照如下規則轉換的:

        json        -->           python
        object                    dict
        array                     list
        string                    str
        number(int)               int
        number(real)              float
        true                      True
        false                     False
        null                      None

    排序功能使得存儲的數據更加有利於觀察,也使得對json輸出的對象進行比較:
    import json
    data1 = {'b':789,'c':456,'a':123}
    data2 = {'a':123,'b':789,'c':456}
    d1 = json.dumps(data1,sort_keys=True)
    d2 = json.dumps(data2)
    d3 = json.dumps(data2,sort_keys=True)
    print(d1)
    print(d2)
    print(d3)
    print(d1==d2)
    print(d1==d3)

    #輸出:
    #{"a": 123, "b": 789, "c": 456}
    #{"a": 123, "c": 456, "b": 789}
    #{"a": 123, "b": 789, "c": 456}
    #False
    #True

    indent參數是縮進的意思:
    import json
    testA = {'name' : 'hongten',
             'age' : '20',
             'gender' : 'M'}
    test_dump = json.dumps(testA, sort_keys = True, indent = 4)
    print(test_dump)

    #輸出:
    #{
    #    "age": "20", 
    #    "gender": "M", 
    #    "name": "hongten"
    #}

    

##################################################
[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}]
[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}]
[True, False, None]
[true, false, null]
hongten
"hongten"
hongten
{"a": 123, "b": 789, "c": 456}
{"b": 789, "c": 456, "a": 123}
{"a": 123, "b": 789, "c": 456}
False
True
{
    "age": "20", 
    "gender": "M", 
    "name": "hongten"
}
>>> 

==================================================

代碼部分:

==================================================

  1 #python json
  2 
  3 #Author   :   Hongten
  4 #Mailto   :   hongtenzone@foxmail.com
  5 #Blog     :   http://www.cnblogs.com/hongten
  6 #QQ       :   648719819
  7 #Version  :   1.0
  8 #Create   :   2013-08-29
  9 
 10 import json
 11 
 12 __doc__ = '''
 13     JSON(JavaScript Object Notation)是一種輕量級的數據交換
 14     格式。易於人閱讀和編寫,同時也易於機器解析和生成。
 15 
 16     在python中,json模塊提供的dumps()方法可以對簡單的數據進行編碼:
 17     import json
 18     
 19     obj = [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy',(88, 42, 'hongten'), {'name' : 'hongten'}]
 20     encodedjson = json.dumps(obj)
 21     print(repr(obj))
 22     print(encodedjson)
 23 
 24     #輸出:
 25     #[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}]
 26     #[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}]
 27 
 28     objA = [True, False, None]
 29     encodedjsonA = json.dumps(objA)
 30     print(repr(objA))
 31     print(encodedjsonA)
 32 
 33     #輸出:
 34     #[True, False, None]
 35     #[true, false, null]
 36 
 37     在json的編碼過程中,會存在從python原始類型向json類型的轉換過程,具體的轉換
 38     如下:
 39 
 40         python      -->           json
 41         dict                      object
 42         list,tuple                array
 43         str,unicode               string
 44         int,long,float            number
 45         True                      true
 46         False                     false
 47         None                      null
 48 
 49     json轉換為python數據類型:
 50     import json
 51     testB = 'hongten'
 52     dump_test = json.dumps(testB)
 53     print(testB)
 54     print(dump_test)
 55     load_test = json.loads(dump_test)
 56     print(load_test)
 57 
 58     #輸出:
 59     #hongten
 60     #"hongten"
 61     #hongten
 62     
 63     而json轉換為python類型的時候,調用的是json.loads()方法,按照如下規則轉換的:
 64 
 65         json        -->           python
 66         object                    dict
 67         array                     list
 68         string                    str
 69         number(int)               int
 70         number(real)              float
 71         true                      True
 72         false                     False
 73         null                      None
 74 
 75     排序功能使得存儲的數據更加有利於觀察,也使得對json輸出的對象進行比較:
 76     import json
 77     data1 = {'b':789,'c':456,'a':123}
 78     data2 = {'a':123,'b':789,'c':456}
 79     d1 = json.dumps(data1,sort_keys=True)
 80     d2 = json.dumps(data2)
 81     d3 = json.dumps(data2,sort_keys=True)
 82     print(d1)
 83     print(d2)
 84     print(d3)
 85     print(d1==d2)
 86     print(d1==d3)
 87 
 88     #輸出:
 89     #{"a": 123, "b": 789, "c": 456}
 90     #{"a": 123, "c": 456, "b": 789}
 91     #{"a": 123, "b": 789, "c": 456}
 92     #False
 93     #True
 94 
 95     indent參數是縮進的意思:
 96     import json
 97     testA = {'name' : 'hongten',
 98              'age' : '20',
 99              'gender' : 'M'}
100     test_dump = json.dumps(testA, sort_keys = True, indent = 4)
101     print(test_dump)
102 
103     #輸出:
104     #{
105     #    "age": "20", 
106     #    "gender": "M", 
107     #    "name": "hongten"
108     #}
109 
110     
111 '''
112 
113 print(__doc__)
114 print('#' * 50)
115 #使用json.dumps()方法對簡單數據進行編碼
116 obj = [['a', 'b', 'c'], 1, 3, 4, 'good', 'boy',(88, 42, 'hongten'), {'name' : 'hongten'}]
117 encodedjson = json.dumps(obj)
118 print(repr(obj))
119 print(encodedjson)
120 
121 #[['a', 'b', 'c'], 1, 3, 4, 'good', 'boy', (88, 42, 'hongten'), {'name': 'hongten'}]
122 #[["a", "b", "c"], 1, 3, 4, "good", "boy", [88, 42, "hongten"], {"name": "hongten"}]
123 
124 
125 objA = [True, False, None]
126 encodedjsonA = json.dumps(objA)
127 print(repr(objA))
128 print(encodedjsonA)
129 
130 #[True, False, None]
131 #[true, false, null]
132 
133 #測試json轉換為python類型
134 testB = 'hongten'
135 dump_test = json.dumps(testB)
136 print(testB)
137 print(dump_test)
138 load_test = json.loads(dump_test)
139 print(load_test)
140 
141 #輸出:
142 #hongten
143 #"hongten"
144 #hongten
145 
146 
147 #排序測試
148 data1 = {'b':789,'c':456,'a':123}
149 data2 = {'a':123,'b':789,'c':456}
150 d1 = json.dumps(data1,sort_keys=True)
151 d2 = json.dumps(data2)
152 d3 = json.dumps(data2,sort_keys=True)
153 print(d1)
154 print(d2)
155 print(d3)
156 print(d1==d2)
157 print(d1==d3)
158 
159 #輸出:
160 #{"a": 123, "b": 789, "c": 456}
161 #{"a": 123, "c": 456, "b": 789}
162 #{"a": 123, "b": 789, "c": 456}
163 #False
164 #True
165 
166 #測試縮進
167 testA = {'name' : 'hongten',
168          'age' : '20',
169          'gender' : 'M'}
170 test_dump = json.dumps(testA, sort_keys = True, indent = 4)
171 print(test_dump)
172 #輸出:
173 #{
174 #    "age": "20", 
175 #    "gender": "M", 
176 #    "name": "hongten"
177 #}

參考資料:

http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM