2014-09-22 30 views
0

我正在解析json格式的日誌文件, 幷包含key:value對形式的數據。如何解析JSON其中鍵在python中是可變的?

我被困在鑰匙本身變化的地方。請在此代碼,我能夠訪問諸如用戶名,EVENT_TYPE,IP等關鍵看附代碼

問題對我來說是進入「提交」鍵內的值,其中

i4x-IITB-CS101-問題33e4aac93dc84f368c93b1d08fa984fc_2_1是一個可變密鑰,將針對不同的用戶改變,

我怎樣才能訪問它作爲一個變量?

{ 
    "username": "batista",   
    "event_type": "problem_check",  
    "ip": "127.0.0.1", 
    "event": { 
     "submission": { 
      "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": { 
       "input_type": "choicegroup", 
       "question": "", 
       "response_type": "multiplechoiceresponse", 
       "answer": "MenuInflater.inflate()", 
       "variant": "", 
       "correct": true 
      } 
     }, 
     "success": "correct", 
     "grade": 1, 
     "correct_map": { 
      "i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": { 
       "hint": "", 
       "hintmode": null, 
       "correctness": "correct", 
       "npoints": null, 
       "msg": "", 
       "queuestate": null 
      } 
     } 

這是我的代碼如何我解決它:

import json 
import pprint 
with open("log.log") as infile: 
# Loop until we have parsed all the lines. 
for line in infile: 
    # Read lines until we find a complete object 
    while (True): 
     try: 
      json_data = json.loads(line) 

      username = json_data['username'] 
      print "username :- " + username 

     except ValueError:     
      line += next(infile) 

如何訪問i4x-IITB-CS101-問題33e4aac93dc84f368c93b1d08fa984fc_2_1鍵和

這個關鍵內部的數據? ?

回答

0

假設你有d = {"a":"b"}類型的詞典然後d.popitem()會給你一個元組("a","b")這是(key,value)。所以使用這個你可以在不知道密鑰的情況下訪問鍵值對。

在你情況下,如果j是主詞典然後j["event"]["submission"].popitem()會給你的元組

("i4x-IITB-CS101-problem-33e4aac93dc84f368c93b1d08fa984fc_2_1": { 
       "input_type": "choicegroup", 
       "question": "", 
       "response_type": "multiplechoiceresponse", 
       "answer": "MenuInflater.inflate()", 
       "variant": "", 
       "correct": true 
      }) 

希望這是你在問什麼。

1

你並不需要預先知道鍵,你可以簡單地在字典迭代:

for k,v in obj['event']['submission'].iteritems(): 
    print(k,v) 
0

使用Python的json模塊,你會解析值從上面的JSON字典結束數據

import json 
parsed = json.loads(this_sample_data_in_question) 
# parsed is a dictionary, so are "correct_map" and "submission" dictionary keys within "event" key 

所以,你可以遍歷鍵,該數據作爲普通字典的值,這樣說:

for k, v in parsed.items(): 
    print k, v 

現在你能找到一個快速的方法是這樣的「i4x-IITB-CS101-問題33e4aac93dc84f368c93b1d08fa984fc_2_1」鍵(可能不同的值):

import json 

parsed = json.loads(the_data_in_question_as_string) 
event = parsed['event'] 

for key, val in event.items(): 
    if key in ('correct_map', 'submission'): 
     section = event[key] 
     for possible_variable_key, its_value in section.items(): 
      print possible_variable_key, its_value 

當然也有可能是遍歷的更好的辦法字典,但你可以根據自己的編碼品位選擇一個,或者如果你的數據類型比這裏發佈的數據大一些,就可以選擇性能。

+0

感謝回覆farzad 我已經添加了我的代碼,請檢查它,我該如何適合您的代碼? – rajsinghaniaful 2014-09-22 10:25:32

+0

上面的示例代碼將適用於問題中提到的問題。變量「its_value」是你的問題的答案。它是一個字典,具有「提示」,「hintmode」鍵......但我不認爲你正在閱讀日誌文件行的方式是正確的。 True_和_next(infile)_沒有必要。 _for_循環將讀取文件的所有行並嘗試解析每行的JSON內容。 – farzad 2014-09-22 15:07:28