JSON中另一种形式的Object文字?

时间:2016-03-03 02:47:04

标签: javascript jquery json firebase

我正在试图弄清楚如何使这项工作:

from graphics import *


def main():



    width = 300
    height = 150
    win = GraphWin("Caesar Cipher, Chandler Long")


    buttonPoint1 = Point(width/2-80, 9*height/10-15)
    buttonPoint2 = Point(width/2+80, 9*height/10+15)
    button1 = Rectangle(buttonPoint1, buttonPoint2)
    labelB = Text(Point(width/2, 9*height/10), "Cipher")
    label1 = Text(Point(width/6, height/5), "Enter Message:")
    label2 = Text(Point(width/6, 2*height/5),  "Key:")
    label3 = Text(Point(width/6, 3*height/5), "Your Message:")
    labelV = Text(Point(2*width/3, 3*height/5), "")
    input1 = Entry(Point(2*width/3, height/5), width//20)
    input2 = Entry(Point(2*width/3, 2*height/5), 3)
    graphOb = [button1, labelB, label1, label2, input1, input2]
    for graphicsObject in graphOb:
        graphicsObject.draw(win)

    while not contains(buttonPoint1, buttonPoint2, win.getMouse()):
        None
    labelB.setText("Quit")

    try:
        labelV.setText(cipher(textBox1.getText(), int(textBox2.getText())))
    except:
        label3.setText('Error:')
        labelV.setText('Invalid key.')
    label3.draw(win)
    labelV.draw(win)

    while not contains(buttonPoint1, win.getMouse()):
        None
    win.close()

def contains(pt1, pt2, ptIn):
    return pt1.getX() < ptIn.getX() and ptIn.getX() < pt2.getX()
    pt1.getY() < ptIn.getY() and ptIn.getY() < pt2.getY()

def encrypt(message, key):
    cleartext = ""
    letterLow = "abcdefghijklmnopqrstuvwxyz"
    letterCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for char in text:
        if char in lettersLow:
            cleartext = cleartext + chr((ord(char) + key - ord("a")) % 26 + ord("a"))
        elif char in letterCap:
            cleartext = cleartext + chr((ord(char) + key - ord("A")) % 26 + ord("A"))
        else:
            cleartext = cleartext + char
    return cleartext


#end main
main()

我可以获得所有变量,但问题是我想将它传递给set()方法,如上所示。整个节点的名称应该是用户名的值,而不是用户名。

我尝试过以这种方式使用文字:

var emails = $("#register_Email").val();
var split = emails.split("@");
var username = split[0];
var first = $("#register_FirstName").val();
var last = $("#register_LastName").val();
var phone = $("#register_PhoneNumber").val();

ref2.set({
   username: {
      active: true,
      email: emails,
      first_name: first,
      last_name: last,
      phone: phone,
      role: "technician"
    }
});

但这不起作用。它只是崩溃,说我不能将ref2.set({ [username]: { active: true, email: emails, first_name: first, last_name: last, phone: phone, role: "technician" } }); [作为变量名传递。

enter image description here

1 个答案:

答案 0 :(得分:1)

不能用object literal来做。但是你可以记住,foo.bar等同于foo['bar'],而是写下来:

var opts = {};
opts[username] = {
      active: true,
      email: emails,
      first_name: first,
      last_name: last,
      phone: phone,
      role: "technician"
    };
ref2.set(opts);