gpt4 book ai didi

javascript - 将 JSON 字符串转换为另一种格式

转载 作者:行者123 更新时间:2023-11-30 11:42:31 25 4
gpt4 key购买 nike

我有一个如下格式的json,我想把这个json转换成问题末尾提到的格式。我只想使用 javascript 来执行此转换,而不是任何其他工具,如 Gson 或服务端 Java 代码。请让我知道这是如何实现的。

 [{
"innNumber": {
"id": "0111",
"idAsLong": 111,
"idWithoutZero": "111"
},
"nightlyRates": [{
"date": "2017-02-09",
"nightlyRate": {
"amount": 155.85,
"currencyCode": "CAD"
},
"points": 0
}]
}, {
"innNumber": {
"id": "0111",
"idAsLong": 111,
"idWithoutZero": "111"
},
"nightlyRates": [{
"date": "2017-02-09",
"nightlyRate": {
"amount": 155.85,
"currencyCode": "CAD"
},
"points": 0
}]

}]

JSON 响应应采用以下要求的格式。

    [{"hotelRoomRate":{
"innNumber": {
"id": "0111",
"idAsLong": 111,
"idWithoutZero": "111"
},
"nightlyRates": [{
"date": "2017-02-09",
"nightlyRate": {
"amount": 155.85,
"currencyCode": "CAD"
},
"points": 0
}]

}
},
{"hotelRoomRate":
{
"innNumber": {
"id": "0111",
"idAsLong": 111,
"idWithoutZero": "111"
},
"nightlyRates": [{
"date": "2017-02-09",
"nightlyRate": {
"amount": 155.85,
"currencyCode": "CAD"
},
"points": 0
}]

}
}]

最佳答案

一个简单的map()会做的

var res = data.map(function(item){
return { hotelRoomRate : item};
});

关于javascript - 将 JSON 字符串转换为另一种格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42099063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com