[python] json 처리

python 2016. 1. 5. 14:14


python에서 json 처리 예제. 




>>> import json


>>> json_string = '{"master":"1.1.1.1:1111", "slave":["2.2.2.2:2222", "3.3.3.3:3333"]}'


>>> decoded = json.loads(json_string)


>>> print json.dumps(decoded, indent=2)

{

  "slave": [

    "2.2.2.2:2222",

    "3.3.3.3:3333"

  ],

  "master": "1.1.1.1:1111"

}


>>> print json.dumps(decoded, indent=2, sort_keys=True)

{

  "master": "1.1.1.1:1111",

  "slave": [

    "2.2.2.2:2222",

    "3.3.3.3:3333"

  ]

}



>>> print decoded['master']

1.1.1.1:1111


>>> print decoded['slave']

[u'2.2.2.2:2222', u'3.3.3.3:3333']


>>> print decoded['slave'][0]

2.2.2.2:2222


>>> print decoded['slave'][1]

3.3.3.3:3333

Posted by '김용환'

댓글을 달아 주세요