如何在python中将JSON文档作为用户输入

时间:2015-08-23 02:57:29

标签: mongodb python-2.7 pymongo database

我正在使用python的mongoDB。我希望用户以JSON格式输入文档,以便我可以将其插入到我的数据库中的某个集合中。如何做到这一点?

2 个答案:

答案 0 :(得分:0)

如同类问题所示,Choosing a file in Python with simple Dialog

from Tkinter import Tk  
from tkFileDialog import askopenfilename  

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing  
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file  
print(filename)

答案 1 :(得分:0)

import json
#read string input 
dataone= raw_input()  
m=json.loads(dataone)
print (m["attribute name in double quotes "])

如果用户输入此字符串{"名称":"乔","年龄":18,"爱好":&# 34;歌唱"} m [" name"]将给出输出joe。