[
{
"SALE_PRICE": "$699.95"
},
{
"SALE_PRICE": "$89.95"
},
{
"SALE_PRICE": "$399.95"
},
{
"SALE_PRICE": "$969.95"
},
{
"SALE_PRICE": "$1,563.42"
},
{
"SALE_PRICE": "$299.95"
},
{
"SALE_PRICE": null
},
{
"SALE_PRICE": "$429.95"
},
{
"ORIGINAL_PRICE": null
},
{
"SALE_PRICE": "$529.95"
}
]
OP的代码评论答案:
这是我使用的整个代码
from tkinter import *
import json
data = ""
priceList = ""
with open('C:\RecoveryImage\\data.json') as json_data:
data = json.load(json_data)
data = json.dumps(data, indent=1)
priceList = data[0]["SALE_PRICE"]
root = Tk()
root.title("Price Viewer")
Display = Frame(root)
Display.pack()
text = Label(Display, text= data) text.pack()
root.mainloop()
答案 0 :(得分:0)
您必须先将字符串转换为等效的json结构
import json
data = json.loads(file_data)
print data[0]["SALE_PRICE"] #Will now print the value
另外,我可以看到你使用了“null”。
在文件中使用“无”等效于“null”的python。
答案 1 :(得分:0)
这是相关代码的格式化版本,带有我的注释:
from tkinter import *
import json
data = "" ### redundant statement
priceList = "" ### redundant statement
with open('C:\RecoveryImage\\data.json') as json_data:
data = json.load(json_data) ### This 'data' refers to a list of dicts.
data = json.dumps(data, indent=1) ### This statement is the culprit.
priceList = data[0]["SALE_PRICE"] ### Expecting `data` to be a list of dicts, not a str
root = Tk() # etc etc
所有声明都是为了生成(希望)语法相同的原始json_data
副本而不使用它。不幸的是,它还会导致data
引用冗余信息。
删除罪魁祸首,或者以json_data_2 =
代替data =
开头。