我對Python真的很陌生,但是我選擇了一個與工作相關的問題,我想我應該如何去做,我會一路學習。在Python 2.6中使用JSON?
我有一個目錄完整的JSON格式的文件。我已經儘可能地將目錄中的所有內容導入到列表中,並遍歷列表來做一個簡單的打印來驗證我獲得了數據。
我想弄清楚如何實際使用Python中給定的JSON對象。在JavaScript中,它的操作非常簡單
var x = {'asd':'bob'}
alert(x.asd) //alerts 'bob'
訪問對象的各種屬性是簡單的點符號。什麼是Python的等價物?
所以這是我的代碼,正在進行導入。我想知道如何處理存儲在列表中的單個對象。
#! /usr/local/bin/python2.6
import os, json
#define path to reports
reportspath = "reports/"
# Gets all json files and imports them
dir = os.listdir(reportspath)
jsonfiles = []
for fname in dir:
with open(reportspath + fname,'r') as f:
jsonfiles.append(json.load(f))
for i in jsonfiles:
print i #prints the contents of each file stored in jsonfiles
閱讀http://docs.python.org/library/json.html – jldupont 2009-11-12 21:58:24
如果有人想在Python年齡超過2.6 JSON的工作,我建議'simplejson'模塊。官方的JSON支持是從'simplejson'改編的,所以它們應該非常相似。 http://code.google.com/p/simplejson/ – steveha 2009-11-12 23:14:35