如何为json动态生成id

时间:2014-02-04 16:48:08

标签: javascript jquery json handlebars.js

我的json部分是这样的:

 {
    "INFO": {
        "01": {
            "Your Name": "Text"
        },
        "02": {
            "Date  Of Birth": "Text"
        },
        "03": {
            "Married": "YesNo"
        },
        "04": {
            "Gender M/F": "Radiobutton"
        }
    },
    "INFO SPOUSE": {
        "01": {
            "Your Name": "Text"
        },
        "02": {
            "Date  Of Birth": "Text"
        },
        "03": {
            "Married": "YesNo"
        },
        "04": {
            "Gender M/F": "Radiobutton"
        }
    }
}

Json在前端工作正常。

现在,我想为每个问题生成上述json的动态id,以便我可以将id的值用于后端目的。

我希望基于问题编号的id,以便在后端我会得到像(你的名字= xxx)

的值

使用handlebarrs.js

1 个答案:

答案 0 :(得分:0)

只是解释(评论框不够)

my mean to say that do you like to get your JSON in the given format {
"INFO": [
    {
        "id": "1",
        "Your Name": "Text"
    },
    {
        "id": "2",
        "Date  Of Birth": "Text"
    },
    {
        "id": "3",
        "Married": "YesNo"
    },
    {
        "id": "4",
        "Gender M/F": "Radiobutton"
    }
],
"INFO SPOUSE": [
    {
        "id": "1",
        "Your Name": "Text"
    },
    {
        "id": "3",
        "Date  Of Birth": "Text"
    },
    {
        "id": "3",
        "Married": "YesNo"
    },
    {
        "id": "4",
        "Gender M/F": "Radiobutton"
    }
]

}

如果是,那么你可以实现它,如果在php中(只需通过迭代对象数组(可以通过使用json_decode($ yourjsonstring,true)函数转换为计划数组)并在迭代后创建一个新数组通过将键值分配给位于info和Info spouse数组内部的id键,希望你能得到我想在这里说的意思。