您好我在从数据库中获取的JSON数据中显示特定值时遇到问题我想在输入文本上只显示产品名称该怎么做?我尝试了,但它说的是不值钱。
这是我的JSON代码: -
import Tkinter as tk
def high_score():
def get_high_score():
# Default high score
high_score = 0
# Try to read the high score from a file
try:
high_score_file = open("high_score.txt", "r")
high_score = int(high_score_file.read())
high_score_file.close()
print("The high score is", high_score)
except IOError:
# Error reading file, no high score
print("There is no high score yet.")
except ValueError:
# There's a file there, but we don't understand the number.
print("I'm confused. Starting with no high score.")
return high_score
# See if we have a new high score
if current_score > high_score:
# We do! Save to disk
print("Yea! New high score!")
save_high_score(current_score)
else:
print("Better luck next time.")
def main():
""" Main program is here. """
# Get the high score
high_score = get_high_score()
# Get the score from the current game
current_score = 0
try:
# Ask the user for his/her score
current_score = int(input("What is your score? "))
except ValueError:
# Error, can't turn what they typed into a number
print("I don't understand what you typed.")
if __name__ == "__main__":
main()
# Make a top level Tk window
root = tk.Tk()
root.title("Cracker GUI v.01")
#Make button which takes highscore as a command
mega_button = tk.Button(root, text="Highscore", command=high_score)
mega_button.pack(side=tk.LEFT)
button = tk.Button(root, text='Quit', width=10, command=root.destroy)
button.pack()
#Lets get the show on the road
root.mainloop()
这是我的功能: -
{"brcode":[{"id":"1","name":"Rolex Watch","description":"Rolex wrist watches","uom":"1000","brand":"1051","tax_id":"1","purchase_price":"5000","sale_price":"6000","barcode":"ROLEX5000","created_date":"2017-05-29 13:31:24","created_by":"1","updated_date":"2017-05-29 13:31:24","updated_by":"1","status":null},{"id":"3","name":"motorola X play","description":"Moto X play mobile phone","uom":"50","brand":"7845","tax_id":"1","purchase_price":"20000","sale_price":"25000","barcode":"MOTOXPLAY500","created_date":"2017-05-29 14:18:43","created_by":"0","updated_date":"2017-05-29 14:18:43","updated_by":"0","status":null},{"id":"4","name":" LG Smart LED TV","description":"Smart LED TV from LG","uom":"5","brand":"5420","tax_id":"1","purchase_price":"80000","sale_price":"100000","barcode":"LGSMART5012","created_date":"2017-05-29 15:39:35","created_by":"0","updated_date":"2017-05-29 15:39:35","updated_by":"0","status":null},{"id":"5","name":"Computer Intel","description":"Intel smart computer","uom":"1","brand":"1","tax_id":"1","purchase_price":"40000","sale_price":"50000","barcode":"INTELPC2123","created_date":"2017-05-29 17:59:31","created_by":"0","updated_date":"2017-05-29 17:59:31","updated_by":"0","status":null},{"id":"6","name":"DDR4 Ram","description":"Ram for computer","uom":"6","brand":"1","tax_id":"1","purchase_price":"4000","sale_price":"5000","barcode":"RAMDDR4","created_date":"2017-05-29 18:15:17","created_by":"0","updated_date":"2017-05-29 18:15:17","updated_by":"0","status":null}]}
请忽略我编写的注释部分以检查值。请告诉我如何从JSON数据中显示产品名称?
答案 0 :(得分:1)
试试这个
var jsonData = JSON.parse(data);
for(var i=0; i<jsonData.brcode.length; i++) {
var product = jsonData.brcode[i];
console.log(product.name);
}
答案 1 :(得分:0)
这是循环遍历JSON以获取产品名称和条形码的基本循环。这应该可以帮助您实现您想要做的事情。将此代码放在您的控制台记录数据的成功函数中。
var jsonData = JSON.parse(data);
for(var i = 0; i < jsonData.brcode.length; i++){
console.log(jsonData.brcode[i].name);
console.log(jsonData.brcode[i].barcode);
}