2014-05-21 42 views
0

我的Web服務器是一樣的東西,與BaseHTTPServer:如何使用JSON數據過去到Web服務器在Python

def do_POST(self): 
    if None != re.search('/connect/*', self.path): 
     ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) 
    if ctype == 'application/json': 
     length = int(self.headers.getheader('content-length')) 
     data = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) 
     recordID = self.path.split('/')[-1]                               
     LocalData.records[recordID] = data 
     print data                           
    else: 
     data = {} 
     self.send_response(200) 
     self.end_headers() 

我如何炒股JSON信息接收到一個Python結構和Python struture以JSON請?我看到我可以如何使用HTTP,例如標題,但不知道如何使用JSON庫存。我需要幫助。謝謝。

親切

阿爾諾

回答

1

要從CGI數據得到一個Python對象:

進口JSON

OBJ = json.loads(數據)

走另一條路:

json_str =傳入json.dump(OBJ)

相關問題