[关闭]
@nalan90 2017-07-31T11:57:26.000000Z 字数 3185 阅读 576

专题十 json库常用操作

Python高效编程技巧实战


常用函数

代码示例
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import json
  4. json_data = {
  5. "_id": "597afc86037ae2cf099b7675",
  6. "index": 1,
  7. "guid": "cf490eac-aa4b-457f-9885-15cc561a6398",
  8. "isActive": False,
  9. "balance": "$1,549.54",
  10. "picture": "http://placehold.it/32x32",
  11. "age": 33,
  12. "latitude": -35.199476,
  13. "longitude": 42.811808,
  14. "tags": [
  15. "fugiat",
  16. "ex",
  17. "incididunt",
  18. "voluptate",
  19. "veniam",
  20. "magna",
  21. "ea"
  22. ],
  23. "friends": [
  24. {
  25. "id": 0,
  26. "name": "Snider Cummings"
  27. },
  28. {
  29. "id": 2,
  30. "name": "Vance Wilder"
  31. }
  32. ],
  33. "favoriteFruit": "strawberry"
  34. }
  35. print type(json_data)
  36. print 'convert python object to json string'
  37. ## 将python object转化成json string
  38. json_str = json.dumps(json_data)
  39. print json_str
  40. ## 将python object json化处理后写入文件
  41. print 'convert python object to json strings then write them to file'
  42. with open('json.demo','w+') as f:
  43. json.dump(json_data,f)
  44. print '-' * 100
  45. ## 将json string 转化成python object
  46. print json.loads(json_str)
  47. ## 从文件中读取json string,并转化成python object
  48. print 'read json strings from file then convert them to python object'
  49. with open('json.demo','r+') as f:
  50. print json.load(f)
  51. ---------------------------------------------------------------------------------------
  52. ## 运行结果如下:
  53. (/anaconda) work:python ys$ python json_demo.py
  54. <type 'dict'>
  55. ## convert python object to json string
  56. {"picture": "http://placehold.it/32x32", "tags": ["fugiat", "ex", "incididunt", "voluptate", "veniam", "magna", "ea"], "guid": "cf490eac-aa4b-457f-9885-15cc561a6398", "friends": [{"id": 0, "name": "Snider Cummings"}, {"id": 2, "name": "Vance Wilder"}], "isActive": false, "index": 1, "_id": "597afc86037ae2cf099b7675", "age": 33, "longitude": 42.811808, "favoriteFruit": "strawberry", "latitude": -35.199476, "balance": "$1,549.54"}
  57. --------------------------------------------------------------------------------------
  58. ## convert json string to python object
  59. {u'picture': u'http://placehold.it/32x32', u'_id': u'597afc86037ae2cf099b7675', u'index': 1, u'age': 33, u'tags': [u'fugiat', u'ex', u'incididunt', u'voluptate', u'veniam', u'magna', u'ea'], u'longitude': 42.811808, u'favoriteFruit': u'strawberry', u'latitude': -35.199476, u'balance': u'$1,549.54', u'guid': u'cf490eac-aa4b-457f-9885-15cc561a6398', u'friends': [{u'id': 0, u'name': u'Snider Cummings'}, {u'id': 2, u'name': u'Vance Wilder'}], u'isActive': False}
  60. ## read data from file and convert to python object
  61. {u'picture': u'http://placehold.it/32x32', u'_id': u'597afc86037ae2cf099b7675', u'index': 1, u'age': 33, u'tags': [u'fugiat', u'ex', u'incididunt', u'voluptate', u'veniam', u'magna', u'ea'], u'longitude': 42.811808, u'favoriteFruit': u'strawberry', u'latitude': -35.199476, u'balance': u'$1,549.54', u'guid': u'cf490eac-aa4b-457f-9885-15cc561a6398', u'friends': [{u'id': 0, u'name': u'Snider Cummings'}, {u'id': 2, u'name': u'Vance Wilder'}], u'isActive': False}
  62. ## 文件中json string
  63. (/anaconda) work:python ys$ cat json.demo
  64. {"picture": "http://placehold.it/32x32", "tags": ["fugiat", "ex", "incididunt", "voluptate", "veniam", "magna", "ea"], "guid": "cf490eac-aa4b-457f-9885-15cc561a6398", "friends": [{"id": 0, "name": "Snider Cummings"}, {"id": 2, "name": "Vance Wilder"}], "isActive": false, "index": 1, "_id": "597afc86037ae2cf099b7675", "age": 33, "longitude": 42.811808, "favoriteFruit": "strawberry", "latitude": -35.199476, "balance": "$1,549.54"}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注