HarmonyOS 编码过的URL在放入map时,会被自动解码

编码过的URL在放入map时会被自动解码,如何能让其保持编码状态存入map呢?

import { url } from '@kit.ArkTS'; 
 
function parseUrlParam(parseUrl: string): url.URLParams | undefined { 
  try { 
    let parsedUrl = url.URL.parseURL (parseUrl) 
    console.info('parseUrl111:' + parsedUrl.params.toString()); 
    return parsedUrl?.params 
  }catch (err) { 
    return undefined 
  } 
} 
 
function parseUrlParamToMap(parseUrl: string): Map<string, Object> { 
  let param = parseUrlParam(parseUrl) 
  console.info('parseUrl222:' + param?.toString()); 
  const map: Map<string, Object> = new Map() 
  if (!param) { 
    return map 
  } 
  param.forEach((value, key)=>{ // 放入map的时候,url会被解码 
    console.info('parseUrl333, key:' + key + ", value:" + value); 
    map.set(key, value) 
  }) 
  return map 
} 
 
@Entry 
@Component 
struct ParseUrlTest { 
  build() { 
    RelativeContainer() { 
      Button('ParseURL', { stateEffect: true, type: ButtonType.Capsule }) 
        .width('80%') 
        .height(40) 
        .margin(10) 
        .onClick(() => { 
          let urlMap = parseUrlParamToMap('lianjiabeike://web/main?url=https%3A%2F%2Fc-bplus.ke.com%2Fc-h5%2Fmap%3Flat%3D32.415593%26lng%3D119.592545%26tabIndex%3D0%26housedelCode%3D103132780520%26from_source%3Dzhoubian%26detailData%3D%257B%2522title%2522%253A%2522%25E9%2587%2591%25E5%259F%259F%25C2%25B7%25E5%259C%25B0%25E4%25B8%25AD%25E6%25B5%25B7%2B2%25E5%25AE%25A42%25E5%258E%2585%2B%25E5%258D%2597%2B...%2522%252C%2522href%2522%253A%2522https%253A%252F%252Fc-bplus.ke.com%252Fc-h5%252Fdetail%253FhousedelCode%253D103132780520%2526cityId%253D321000%2526hidenav%253D2%2526immersive%253Dtrue%2526pagetype%253DBplusapph5detail%2526from_type%253Dclick%2526view_type%253Drank%2526from_page%253DcFeed%2526parentSceneId%253D959671633515267600%2522%252C%2522desc%2522%253A%2522%25E6%25B1%259F%25E9%2583%25BD%25E5%258C%25BA%2B58.8%25E4%25B8%2587%25E5%2585%2583%2B60%25E5%25B9%25B3%25E7%25B1%25B3%2522%252C%2522headerImg%2522%253A%2522https%253A%252F%252Fimg.ljcdn.com%252F110000-inspection%252F4f2f6bbb23fa2c46da3ffae0e2e8158e-032.jpg%2521m_fit%252Cw_720%252Cl_fbk%252Clx_random%252Cly_random%252Cls_50%252Cf_jpg%2522%257D') 
          console.info('parseUrl444:' + urlMap.get("url")); 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
HarmonyOS
16h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

​URLParams的forEach方法规则就是会将value值解码,如需取编码的值,可以调用encodeURIComponent(value)进行再编码

将源字符串的key和value解码一次,比较解码前后的值,如果相同,则源字符串未编码,直接插入map,反之则源字符串是编码过的,需编码一次后再插入map。可参考如下代码:​

import { url } from '@kit.ArkTS'; 
 
function parseUrlParam(parseUrl: string): url.URLParams | undefined { 
  try { 
    let parsedUrl = url.URL.parseURL (parseUrl) 
    return parsedUrl?.params 
  }catch (err) { 
    return undefined 
  } 
} 
 
function parseUrlParamToMap(parseUrl: string): Map<string, Object> { 
  let param = parseUrlParam(parseUrl) 
  const map: Map<string, Object> = new Map() 
  if (!param) { 
    return map 
  } 
 
  param.forEach((value, key)=>{ 
    if (decodeURIComponent(key) == key && decodeURIComponent(value) == value) { 
      map.set(key, value) 
    } else { 
      map.set(encodeURIComponent(key), encodeURIComponent(value)) 
    } 
  }) 
  return map 
} 
 
@Entry 
@Component 
struct ParseUrlTest { 
  build() { 
    RelativeContainer() { 
      Button('ParseURL', { stateEffect: true, type: ButtonType.Capsule }) 
        .width('80%') 
        .height(40) 
        .margin(10) 
        .onClick(() => { 
          let urlMap = parseUrlParamToMap('lianjiabeike://web/main?url=https%3A%2F%2Fc-bplus.ke.com%2Fc-h5%2Fmap%3Flat%3D32.415593%26lng%3D119.592545%26tabIndex%3D0%26housedelCode%3D103132780520%26from_source%3Dzhoubian%26detailData%3D%257B%2522title%2522%253A%2522%25E9%2587%2591%25E5%259F%259F%25C2%25B7%25E5%259C%25B0%25E4%25B8%25AD%25E6%25B5%25B7%2B2%25E5%25AE%25A42%25E5%258E%2585%2B%25E5%258D%2597%2B...%2522%252C%2522href%2522%253A%2522https%253A%252F%252Fc-bplus.ke.com%252Fc-h5%252Fdetail%253FhousedelCode%253D103132780520%2526cityId%253D321000%2526hidenav%253D2%2526immersive%253Dtrue%2526pagetype%253DBplusapph5detail%2526from_type%253Dclick%2526view_type%253Drank%2526from_page%253DcFeed%2526parentSceneId%253D959671633515267600%2522%252C%2522desc%2522%253A%2522%25E6%25B1%259F%25E9%2583%25BD%25E5%258C%25BA%2B58.8%25E4%25B8%2587%25E5%2585%2583%2B60%25E5%25B9%25B3%25E7%25B1%25B3%2522%252C%2522headerImg%2522%253A%2522https%253A%252F%252Fimg.ljcdn.com%252F110000-inspection%252F4f2f6bbb23fa2c46da3ffae0e2e8158e-032.jpg%2521m_fit%252Cw_720%252Cl_fbk%252Clx_random%252Cly_random%252Cls_50%252Cf_jpg%2522%257D') 
          console.info('parseUrl:' + urlMap.get("url")); 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
分享
微博
QQ
微信
回复
7h前
相关问题
HarmonyOS URL特殊字符编码
549浏览 • 1回复 待解决
HarmonyOS TLV格式编码解码
220浏览 • 1回复 待解决
实现文本编码解码机制。
376浏览 • 1回复 待解决
求鸿蒙视频编码解码具体demo
6414浏览 • 1回复 待解决
H5通url scheme拉起对应应用
436浏览 • 1回复 待解决
如何处理应用存在URL编码
812浏览 • 1回复 待解决
HarmonyOS视频解码问题
507浏览 • 1回复 待解决
HarmonyOS map无法展示
98浏览 • 1回复 待解决
HarmonyOS class 转 Map
72浏览 • 1回复 待解决
HarmonyOS CBOR编解码问题
270浏览 • 1回复 待解决