如何在安卓中将 JSON 数组转换为 jsonobject



我想将我的数据发送到服务器,我想将jsonarray转换为jsonobject。我该怎么做。

[
{
"price":"12000",
"problems":"toyota",
"selected":true
},  
{
"price":"10500",
"problems":"KIA",
"selected":true
}
]

{
"price":["12000", "10500"],
"problems":["toyota","KIA"],
"selected":["true","true"]
}

把这段代码放在一个函数中(我把它命名为convert()(,你会得到想要的结果:

function convert() {
/** 
* you can presumably leave out this initialization 
* as you already have this JSON-Array somewhere else in your code. 
* I put it in just for presentational purposes.
*/
const source = [
{
'price': '12000',
'problems': 'toyota',
'selected': true
},
{
'price': '10500',
'problems': 'KIA',
'selected': true
}
];
const destination = { price: [], problems: [], selected: [] };
source.forEach(element => {
destination.price.push(element.price);
destination.problems.push(element.problems);
destination.selected.push(element.selected);
});
}
}

最新更新