我正在尝试重组从API获取的对象。请参阅下面的对象。
{
"App 1": "5a67-b-45-86e7-bfb351",
"App 2": "293-e2-4a-96c-4471d0ea",
"App 3": "f87d5-e0-41-bd-16dc72e"
}
我希望使用Javascript以以下格式重建它:
我有以下数据:
{
"App 1": "5a67-b-45-86e7-bfb351",
"App 2": "293-e2-4a-96c-4471d0ea",
"App 3": "f87d5-e0-41-bd-16dc72e"
}
我希望使用Javascript以以下JSON格式重建它:
[
{
"id" : "5a67-b-45-86e7-bfb351",
"name" : "App 1"
},
{
"id" : "293-e2-4a-96c-4471d0ea",
"name" : "App 2"
},
{
"id":"f87d5-e0-41-bd-16dc72e",
"name":"App 3"
}
]
答案 0 :(得分:1)
您可以使用public class MovieWithDirector {
Movie movie;
Director director;
}
获取json对象所有键的数组,然后遍历所有键并将它们添加到输出数组中,如下所示:
Object.keys
答案 1 :(得分:0)
const obj = {
"App 1": "5a67-b-45-86e7-bfb351",
"App 2": "293-e2-4a-96c-4471d0ea",
"App 3": "f87d5-e0-41-bd-16dc72e"
}
const newObj = []
Object.keys(obj).forEach(key => {
newObj.push({ name: key, id: obj[key] })
})
console.log(newObj)