HarmonyOS 鸿蒙Next 如何将HashMap转换成json再UTF-8编码,再base64编码
HarmonyOS 鸿蒙Next 如何将HashMap转换成json再UTF-8编码,再base64编码
{
osType : 01
appVersion : 1.7.7
dvImei : 864db36640ec680a2b09252fd161f43ec2b10c7d
nonceStr : 20240524105344581032267853167626
osVersion : 14.8.1
txnTm : 20240524105422
appChlNo : 04
}
如何将hasmap转换成json再UTF-8编码,再base64编码
原有项目逻辑如下
headValue = [[NSString alloc] initWithData:[self.headerDict toJSON] encoding:NSUTF8StringEncoding];
[paramDict setValue:[RMSecurityUtil encodeBase64String:headValue] forKey:@“txnHeaderInfo”];
osType : 01
appVersion : 1.7.7
dvImei : 864db36640ec680a2b09252fd161f43ec2b10c7d
nonceStr : 20240524105344581032267853167626
osVersion : 14.8.1
txnTm : 20240524105422
appChlNo : 04
}
如何将hasmap转换成json再UTF-8编码,再base64编码
原有项目逻辑如下
headValue = [[NSString alloc] initWithData:[self.headerDict toJSON] encoding:NSUTF8StringEncoding];
[paramDict setValue:[RMSecurityUtil encodeBase64String:headValue] forKey:@“txnHeaderInfo”];
2 回复
参考如下代码:
import HashMap from '[@ohos](/user/ohos).util.HashMap';
import util from '[@ohos](/user/ohos).util';
function hashMap2Json() {
const hashMap: HashMap<string, string> = new HashMap();
hashMap.set("osType", "01");
hashMap.set("appVersion", "1.7.7");
hashMap.set("dvImei", "864db36640ec680a2b09252fd161f43ec2b10c7d");
hashMap.set("nonceStr", "20240524105344581032267853167626");
hashMap.set("osVersion", "14.8.1");
hashMap.set("txnTm", "20240524105422");
hashMap.set("appChlNo", "04");
const jsonObject: Record<string, Object> = {};
hashMap.forEach((value, key) => {
if (key != undefined && value != undefined) {
jsonObject[key] = value;
}
})
const jsonString = JSON.stringify(jsonObject);
console.log('jsonString', jsonString);
return jsonString;
}
[@Entry](/user/Entry)
[@Component](/user/Component)
struct TestHashMap {
[@State](/user/State) message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
const temp = hashMap2Json();
const json2encodeUtf8 =new util.TextEncoder().encodeInto(temp);
console.log('json2encodeUtf8: ' + json2encodeUtf8);
const utf82Base64 =new util.Base64Helper().encodeToStringSync(json2encodeUtf8);
console.log('utf82Base64: ' + utf82Base64);
})
}
.width('100%')
}
.height('100%')
}
}
在HarmonyOS鸿蒙Next系统中,你可以使用以下步骤将HashMap
转换成JSON字符串,然后进行UTF-8编码,并最终进行Base64编码。以下是一个示例代码:
-
将HashMap转换成JSON字符串: 使用
Gson
库(或者Jackson
等其他JSON库)将HashMap
对象转换成JSON字符串。Gson gson = new Gson(); String jsonString = gson.toJson(hashMap);
-
进行UTF-8编码: 将JSON字符串转换为字节数组,并使用UTF-8编码。
byte[] utf8Bytes = jsonString.getBytes(StandardCharsets.UTF_8);
-
进行Base64编码: 使用Java自带的Base64编码工具将字节数组转换为Base64字符串。
String base64String = Base64.getEncoder().encodeToString(utf8Bytes);
完整示例如下:
Gson gson = new Gson();
String jsonString = gson.toJson(hashMap);
byte[] utf8Bytes = jsonString.getBytes(StandardCharsets.UTF_8);
String base64String = Base64.getEncoder().encodeToString(utf8Bytes);
这段代码将HashMap
转换为JSON字符串,然后进行UTF-8编码和Base64编码。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html