Python3中解析string類型的json對象

一、JSON格式規範

參考網站地址:https://www.w3school.com.cn/json/index.asp

 

二、JSON不同寫法對應的解析方法(import json)

1.第一種寫法類似於字典鍵值對(最基本的寫法)

       string = '{ 
                "status": "error",
                "messages": ["Could not find resource or operation."],
                "code": 404
               }'

             解析方法:

                 anlyze_data = json.loads(string )

                 print(anlyze_data['code'])

           2.第二種寫法數組類型

              string = '{

                         "idlist":[{

                         "id":"172.20.20.1"},{

                         "id":"172.20.20.2"

                        }]

                      }'

              解析方法:         

              anlyze_data = json.loads(string)
              print(anlyze_data['idlist'])
              print(anlyze_data['idlist'][0])
              print(anlyze_data['idlist'][0]['id'])

三、JSON格式規範檢查

         參考網站地址:http://www.bejson.com/

                

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章