search

Home  >  Q&A  >  body text

javascript - Adding multiple levels of brackets to convert JSON format

{
    "1":"文去",
    "2":"147",
}

needs to be changed to

[
    {
        "name": "1", 
        "value": "文去"
    }, 
    {
        "name": "2", 
        "value": "147"
    }
]
曾经蜡笔没有小新曾经蜡笔没有小新2755 days ago655

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:49:32

    Idea: Take out the key and value in the dictionary to form a new res_dic, and append res_dic to the list as an item in res_list.

    res_list = []
    res_dic = {}
    for k,v in dic.items():
        res_dic = {
            'name':v,
            'value':k
            }
        res_list.append(res_dic)

    reply
    0
  • Cancelreply