我解析日志文件是JSON格式,并包含在密钥的形式的数据:值对。
我被困在地方密钥本身是可变的。 请看附代码
在这段代码中,我能够访问诸如用户名,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键
这里面的关键数据?