从嵌套数组JSON访问单个数据

时间:2019-11-14 08:25:36

标签: arrays json

我正在尝试使用URL'https://123.abc.com/student'从JSON文件访问单个数据。我需要显示该URL中的单个数据。有人可以帮我吗?

我的Json数据看起来像这样

    import_file_path = filedialog.askopenfilename() 
    f = pd.read_csv (import_file_path, index_col = 0 , low_memory=False ,encoding="unicode_escape" ,delimiter=',')
    export_path=import_file_path[0:-4]+"_OUTPUT.csv"  #to create the output path
    export_path_ing=import_file_path[0:-4]+"_INGEST_OUTPUT.txt"        

    dirs = list(f.columns.values)

    # remove previous IntVars
    intvar_dict.clear()

    # remove previous Checkboxes
    for cb in checkbutton_list:
        cb.destroy()
    checkbutton_list.clear() 

    for filename in dirs:
        # create IntVar for filename and keep in dictionary
        intvar_dict[filename] = tk.IntVar()

        # create Checkbutton for filename and keep on list
        c = tk.Checkbutton(canvas1, text=filename, variable=intvar_dict[filename])
        c.pack()
        checkbutton_list.append(c)

def test():
    li=[]
    for key, value in intvar_dict.items():
        if value.get() > 0:
            li.append(key)
            print(li)

    f2=f[li]
# --- main -
    f2.to_csv ( export_path,index = None, header=True ,sep=',')
    f2.to_csv (export_path_ing,index = None, header=True ,sep='\t')

intvar_dict = {}
 # to keep all Checkbuttons for all filenames
checkbutton_list = []

window = tk.Tk()
window.title("Column Choices          ~~~~~sarsuman")
window.geometry("500x500")
window.option_add("*Button.Background", "black")
window.option_add("*Button.Foreground", "white")
#frame = tk.Frame(window)
canvas1 = tk.Canvas(window, width = 1000, height = 1000, bg = 'orange',highlightthickness=1)

scrollbar = tk.Scrollbar(window,command=canvas1.yview)
scrollbar.pack(side = 'right', fill ='y')
canvas1.configure(yscrollcommand = scrollbar.set)
canvas1.pack(side='left',expand=True,fill='both')
canvas1.bind('<Configure>', on_configure)
frame = tk.Frame(canvas1)
canvas1.create_window((0,0), window=frame, anchor='nw')

#canvas1.config(xscrollcommand=tk.hbar.set, yscrollcommand=tk.vbar.set)    

#back = tk.Frame(master=window,bg='black')
#back.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
#back.pack(fill=tk.BOTH, expand=1) #Expand the frame to fill the root window

lbl = tk.Label(canvas1, text="Path")
lbl.pack()

ent1 = tk.Entry(canvas1)
ent1.pack()

btn1 = tk.Button(canvas1, text="Select Path", command=browse)
btn1.pack()

btn1 = tk.Button(canvas1, text="Test Checkboxes", command=test)
btn1.pack()
window.mainloop()  
window.quit

2 个答案:

答案 0 :(得分:1)

访问单个数据和数组

let data = {"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john@gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark@gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen@gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay@gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test@gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens@gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6@gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu@cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi@gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi@gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi@gmail.com","mobile":"9042758563","__v":0}]}

//access name
console.log('name--', data.students[0].name)

//single object
console.log(data.students[0])

//access array of objects
data.students.map(d=> console.log(d))

答案 1 :(得分:1)

您可以使用

let data = {"students":[{"_id":"5dc516e51bdd2c0014e32e65","name":"John","email":"john@gmail.com","mobile":"9988776655","__v":0},{"_id":"5dc517b51bdd2c0014e32e69","name":"Mark","email":"mark@gmail.com","mobile":"9834783927","__v":0},{"_id":"5dca9231cfba5c0014d3d765","name":"Glen","email":"glen@gmail.com","mobile":"9996474563","__v":0},{"_id":"5dcb898b9327cb00142169bb","name":"vijay","email":"vijay@gmail.com","mobile":"9884699633","__v":0},{"_id":"5dcb89c09327cb00142169bc","name":"tests","email":"test@gmail.com","mobile":"9887456123","__v":0},{"_id":"5dcb8caa9327cb00142169bd","name":"greens","email":"greens@gmail.com","mobile":"9876543210","__v":0},{"_id":"5dcb95d69327cb00142169be","name":"Akash","email":"a4akash6@gmail.com","mobile":"9846042118","__v":0},{"_id":"5dcb961c9327cb00142169bf","name":"Akash K M","email":"Akash.Madhu@cognizant.com","mobile":"9846042118","__v":0},{"_id":"5dcce611ad69f500149660d5","name":"efavewbvwevb","email":"indrajitrakurajravi@gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce874ad69f500149660d6","name":"efavewbvwevb","email":"indrajitrakurajravi@gmail.com","mobile":"9042758563","__v":0},{"_id":"5dcce968ad69f500149660d7","name":"indrajit","email":"indrajitrakurajravi@gmail.com","mobile":"9042758563","__v":0}]}

let result = data.students.forEach(val=>{
  console.log(val)
  // let data individually as 
  let name = val.name 
  let email = val.email 
})