2015-07-13

PYTHON - How to parse a JSON string

# 1. Original JSON String
text = """{ "count": 2, "record": [ {"lemma":"God"}, {"lemma":"Men"} ] }"""

# 2. Text to JSON object
import json
j = json.loads(text)

# 3. Parse a top-level property
print 'count: ' + str( j['count'] )

# 4. Traverse multiple records
for item in j['record']:
    print( item['lemma'] )

No comments:

Post a Comment