How to parse JSON string in Python

When developing a web service, you may often rely on a JSON-based web service protocol. If you are using Python for that, it is straightforward to deal with JSON-formatted messages due to its extensible modules. For example, Python's JSON module which was introduced in Python 2.6 provides default JSON encoder and decoder in Python. There are also other JSON encoder/decoder that you can install and use (e.g., simplejson).

In this tutorial, I will describe how to parse JSON in Python with JSON module.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

The following code snippet is an example of parsing JSON in Python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import json
 
json_input = '{ "one": 1, "two": { "list": [ {"item":"A"},{"item":"B"} ] } }'
 
try:
    decoded = json.loads(json_input)
 
    # pretty printing of json-formatted string
    print json.dumps(decoded, sort_keys=True, indent=4)
 
    print "JSON parsing example: ", decoded['one']
    print "Complex JSON parsing example: ", decoded['two']['list'][1]['item']
 
except (ValueError, KeyError, TypeError):
    print "JSON format error"

The above Python example will print out the following.

{
    "one": 1,
    "two": {
        "list": [
            {
                "item": "A"
            },
            {
                "item": "B"
            }
        ]
    }
}
JSON parsing example:  1
Complex JSON parsing example:  B
 

 

 

[출처] http://xmodulo.com/how-to-parse-json-string-in-python.html

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
37 윈도우에서 easy_install, pip 사용하기. 졸리운_곰 2015.11.24 1450
36 윈도우에서 파이썬 설치하기 (virtualenv, pip 사용법) file 졸리운_곰 2015.11.24 707
» How to parse JSON string in Python 졸리운_곰 2015.11.10 451
34 windows 에서 python 2.x 와 3.x 동시 설치 사용하기 졸리운_곰 2015.09.17 628
33 Title: HTML Scraper using pyhton 졸리운_곰 2015.09.01 400
32 기계 학습의 A to Z : 인구 변화 시각화와 예측 file 졸리운_곰 2015.05.21 1736
31 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습(문서 분류) file 졸리운_곰 2015.05.21 2402
30 기계 학습의 A to Z : IPython notebook과 scikit-learn을 활용한 기계 학습 file 졸리운_곰 2015.05.21 2006
29 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습 (군집화, Clustering) file 졸리운_곰 2015.05.21 2084
28 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습 (차원 축소) file 졸리운_곰 2015.05.21 2182
27 기계 학습의 A to Z : scikit-learn을 활용한 기계 학습(모델 평가) [1] file 졸리운_곰 2015.05.21 1411
26 Tkinter로 하는 GUI 프로그래밍 file 졸리운_곰 2015.05.12 1628
25 위대한 LG SMART SMA LG 빅데이터 플랫폼 [파이썬 웹 크롤러] 웹 스파이더 file 졸리운_곰 2015.05.02 1154
24 위대한 LG SMART SMA LG 빅데이터 플랫폼 [소셜 웹 마이닝] 데이터 마이닝, 웹 마이닝 file 졸리운_곰 2015.05.02 775
23 Python CGI Programming file 졸리운_곰 2015.04.28 756
22 파이썬(Python)으로 CGI 프로그램 작성하기 졸리운_곰 2015.04.28 1403
21 winglet: 간단한 web crawler 졸리운_곰 2015.04.08 1748
20 python scrapy 예제, [python] Scrapy - 네이버 영화 파싱 졸리운_곰 2014.12.11 1496
19 python Scrapy 사용법 졸리운_곰 2014.12.11 2163
18 python 2.X 와 python 3.X 의 tkinter 코드 호환 유지 기법 : from Tkinter import * 에러시 졸리운_곰 2014.11.03 1767
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED