查看以前的答案,但无法解决,json.loads无法正常工作。
代码:
import json
import operator
Data_to_python ={}
Bank_Data_note= open('Data_for_python.txt','r') # open file
Bank_Data_str = Bank_Data_note.read()
print(Bank_Data_str)
Data_to_python =json.loads(Bank_Data_str) # dictinary
print(Data_to_python)
来自文本文件的json格式:
{{"Transaction_1":{"Name":"Magnolia","Location":"Ayilon male","Amount":289,"Date":"5/5/18"},
{"Transaction_2":{"Name":"Landver,"Location":"Cinima-city Ramat-hashron","Amount":15,"Date":"15/5/18"},
{"Transaction_3":{"Name":"Superfarm","Location":"Shivat-hacochvim male","Amount":199,"Date":"7/5/18"},
{"Transaction_4":{"Name":"Printing solutions","Location":"Afeka tel-aviv","Amount":16,"Date":"25/5/18"}}
我得到了这个:
obj,end = self.scan_once(s,idx)json.decoder.JSONDecodeError: 期望用双引号括起来的属性名称:第1行第2列 (char 1)
答案 0 :(得分:0)
您的JSON数据中有太多大括号!它缺少双引号。这是一个已修复的版本,以及一些json
加载和转储代码来测试它。
import json
data = '''
{
"Transaction_1": {"Name":"Magnolia","Location":"Ayilon male","Amount":289,"Date":"5/5/18"},
"Transaction_2": {"Name":"Landver","Location":"Cinima-city Ramat-hashron","Amount":15,"Date":"15/5/18"},
"Transaction_3": {"Name":"Superfarm","Location":"Shivat-hacochvim male","Amount":199,"Date":"7/5/18"},
"Transaction_4": {"Name":"Printing solutions","Location":"Afeka tel-aviv","Amount":16,"Date":"25/5/18"}
}'''
obj = json.loads(data)
print(obj)
print('- ' * 20)
# Convert back to JSON for nicer printing
print(json.dumps(obj, indent=4))
<强>输出强>
{'Transaction_1': {'Name': 'Magnolia', 'Location': 'Ayilon male', 'Amount': 289, 'Date': '5/5/18'}, 'Transaction_2': {'Name': 'Landver', 'Location': 'Cinima-city Ramat-hashron', 'Amount': 15, 'Date': '15/5/18'}, 'Transaction_3': {'Name': 'Superfarm', 'Location': 'Shivat-hacochvim male', 'Amount': 199, 'Date': '7/5/18'}, 'Transaction_4': {'Name': 'Printing solutions', 'Location': 'Afeka tel-aviv', 'Amount': 16, 'Date': '25/5/18'}}
- - - - - - - - - - - - - - - - - - - -
{
"Transaction_1": {
"Name": "Magnolia",
"Location": "Ayilon male",
"Amount": 289,
"Date": "5/5/18"
},
"Transaction_2": {
"Name": "Landver",
"Location": "Cinima-city Ramat-hashron",
"Amount": 15,
"Date": "15/5/18"
},
"Transaction_3": {
"Name": "Superfarm",
"Location": "Shivat-hacochvim male",
"Amount": 199,
"Date": "7/5/18"
},
"Transaction_4": {
"Name": "Printing solutions",
"Location": "Afeka tel-aviv",
"Amount": 16,
"Date": "25/5/18"
}
}