我正在使用groovy脚本将主体中的一些值转换为它们的原始数据类型。为了实现这一点,我使用了JsonSluper和JsonOutput。JsonSluper和JsonOutput正在将我的json主体中的字符串中的一些符号转换为奇怪的字符。有什么建议可以解决这个问题吗?
示例:“单价”:“每磅99美分”转换为“单价”:“每磅99\u00a2”
1/2转换为
我的脚本:
def body = message.getBody(java.lang.String);
def JSONInput = new JsonSlurper().parseText(body);
JSONInput.data.ProductStorePrices.each{
if(it.Price != null){
it.Price = it.Price.toDouble();
}
if(it.Active != null){
it.Active = it.Active.toBoolean();
}
}
message.setBody(JsonOutput.toJson(JSONInput))
下面是我转换前的JSONBody:
{
"data": {
"ProductStorePrices": [
{
"Product_LegacyID": "1005",
"Store_LegacyStoreID": "2",
"DistributionCenter_LegacyRegionID": "916",
"PriceType_Code": "unadjustedRetail",
"ValidFrom": "2000-01-01T00:00:00Z",
"Price": "9.99",
"ValidTo": "2022-05-04T23:59:59Z",
"Currency": "USD",
"Active": "false",
"UnitPrice": "99¢ per lb.",
"LastChangedAt": "2022-05-04T23:59:59Z"
}
]
}
}
我的输出JSONBody转换后使用上面的groovy脚本
{
"data": {
"ProductStorePrices": [
{
"Product_LegacyID": "1005",
"Store_LegacyStoreID": "2",
"DistributionCenter_LegacyRegionID": "916",
"PriceType_Code": "unadjustedRetail",
"ValidFrom": "2000-01-01T00:00:00Z",
"Price": 9.99,
"ValidTo": "2022-05-04T23:59:59Z",
"Currency": "USD",
"Active": false,
"UnitPrice": "99\u00a2 per lb.",
"LastChangedAt": "2022-05-04T23:59:59Z"
}
]
}
}
1条答案
按热度按时间vlurs2pr1#
这不是奇怪的符号,这是unicode。我使用库com.google.code.gson来实现这些目的。试试这个:
输出:
{"data":{"ProductStorePrices":[{"Active":false,"Currency":"USD","DistributionCenter_LegacyRegionID":"916","LastChangedAt":"2022-05-04T23:59:59Z","Price":9.99,"PriceType_Code":"unadjustedRetail","Product_LegacyID":"1005","Store_LegacyStoreID":"2","UnitPrice":"99¢ per lb.","ValidFrom":"2000-01-01T00:00:00Z","ValidTo":"2022-05-04T23:59:59Z"}]}}