在我的代码的一部分中,我将从JSON文件中提取数据并将其放置在基于对象的数组中。
我成功接收了数据,并且可以将每个数据放在数组中的单个对象上。唯一的问题是,我想给每个对象起一个名字。
class loginUser:
def __init__(self, window, master=None):
self.wind = window
self.wind.title("System F2T")
#Definicoes de fonte p/ o layout de login
self.fonteTitulo = ("Arial","10","bold")
self.fontePadrao = ("Arial", "10")
#Labels e campos de texto do sistema de login
self.userLabel = Label(text="Digite seu usuário:", font=self.fontePadrao,bg="#000",fg="#FFF").place(x=27,y=60)
self.user = Entry(textvariable=self.var, font=self.fontePadrao,bg="#FFF",fg="#000")
self.user.place(x=140,y=60,width=110)
self.senhaLabel = Label(text="Digite sua senha:", font=self.fontePadrao,bg="#000",fg="#FFF").place(x=29,y=90)
self.senha = Entry(textvariable=self.var2, font=self.fontePadrao,show="*",bg="#FFF",fg="#000")
self.senha.place(x=140,y=90,width=110)
self.max_user = 1
self.var = StringVar()
self.var.trace("w", loginUser.limiteUsuario)
self.max_senha = 4
self.var2 = StringVar()
self.var2.trace("w", loginUser.limiteSenha)
def limiteUsuario(self,*args):
u = self.var.get()
if len(u) == 1 and not 65<=ord(u)<=68: # you can also use *if not u in "ABCD"*
self.var.set("")
elif len(u) > 1:
if not 65<=ord(u[-1])<=68: # retirar ultimo caracter caso nao seja digito
self.var.set(u[:-1])
else: # aproveitar apenas os primeiros 5 chars
self.var.set(u[:self.max_user])
def limiteSenha(self,*args):
s = self.var2.get()
if len(s) > 4:
if not s[-1].isdigit(): # retirar ultimo caracter caso nao seja digito
self.var2.set(s[:-1])
else: # aproveitar apenas os primeiros 5 chars
self.var2.set(s[:self.max_senha])
if __name__ == "__main__":
root = Tk()
root['bg'] = "#000"
loginUser(root)
#Tamanho da janela
root.geometry("330x200")
root.mainloop()
var playlist_data = {
"Music1": {
"soundcloud": "Soundcloud Music 1",
"spotify": "Spotify Music 1"
},
"Music2": {
"soundcloud": "Soundcloud Music 2",
"spotify": "Spotify Music 2"
},
"Music3": {
"soundcloud": "Soundcloud Music 3",
"spotify": "Spotify Music 3"
},
"Music4": {
"soundcloud": "Soundcloud Music 4",
"spotify": "Spotify Music 4"
}
};
var links = [];
$.each(playlist_data, function(index, element) {
links.push({
spotify: element.spotify,
soundcloud: element.soundcloud,
});
});
console.log(links);
在上面的代码中,似乎复制了相同的JSON数据,但它是简化版本。
因此,我想将其命名为<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
和links.Music2
答案 0 :(得分:0)
由于Javascript不支持命名索引,因此您必须将links
作为对象才能通过links.Music2.spotify
访问其属性。这很容易做到,但是,它只会为您提供与开始时完全相同的数据:
var playlist_data = {
"Music1": {
"soundcloud": "Soundcloud Music 1",
"spotify": "Spotify Music 1"
},
"Music2": {
"soundcloud": "Soundcloud Music 2",
"spotify": "Spotify Music 2"
},
"Music3": {
"soundcloud": "Soundcloud Music 3",
"spotify": "Spotify Music 3"
},
"Music4": {
"soundcloud": "Soundcloud Music 4",
"spotify": "Spotify Music 4"
}
};
var links = {};
$.each(playlist_data, function(index, element) {
links[index]= {
spotify: element.spotify,
soundcloud: element.soundcloud,
};
});
console.log(links);
console.log(links.Music2.spotify)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
答案 1 :(得分:-1)
您可以使用以下代码命名每个obj
this.letters
答案 2 :(得分:-1)
似乎您想将spotify
用作links
的密钥。
JS中有两种类型的数组:
标准数组为[ ]
关联数组为{ }
!
如您所见,您可以使用{ }
,它在javascript中也是一个对象,它是一个数组。然后,您可以使用spotify
作为密钥。
因此,您的代码将如下所示:
var links = {};
$.each(playlist_data, function(index, element) {
links[index] = {
spotify: element.spotify,
soundcloud: element.soundcloud,
};
});
console.log(links_s.Music1.spotify) // Spotify Music 1