HarmonyOS 鸿蒙Next排序序列化问题

发布于 1周前 作者 eggper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next排序序列化问题
<markdown _ngcontent-pmc-c237="" class="markdownPreContainer">

大佬们 arkts 排序序列化为什么返回{}
let sortQuery = new TreeMap();
sortQuery.set(“ab”,“cc”);
sortQuery.set(“cc”,“cc”);
sortQuery.set(“dd”,“cc”);
${JSON.stringify(sortQuery)} = {}
打印结果是{} 和期望的不一样!!!
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
</markdown>

13 回复

JSON.stringify只能序列化普通的JavaScript对象,而TreeMap是一个特定的数据结构,我们需要将它转换成普通对象。

cke_633.png

参考以下代码:

import { TreeMap } from '@kit.ArkTS';
// 创建一个TreeMap实例并添加一些数据
let sortQuery:TreeMap<string,string>=new TreeMap();
sortQuery.set("ab", "cc");
sortQuery.set("cc", "cc");
sortQuery.set("dd", "cc");

let obj: Record<string, string> = {};

for (let entry of sortQuery.entries()) { obj[Object(entry)[0]] = Object(entry)[1]; }

// 序列化为JSON字符串 let jsonString = JSON.stringify(obj); console.log(jsonString); // {“ab”:“cc”,“cc”:“cc”,“dd”:“cc”}<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

cke_8304.png 

参数拼接示例

import { TreeMap } from '@kit.ArkTS';

@Entry @Component struct Page45 { build() { Column() { Button(‘测试’).onClick(() => { // 创建一个新的 TreeMap let sortQuery: TreeMap<string, string> = new TreeMap(); sortQuery.set(“ab”, “cc”); sortQuery.set(“cc”, “cc”); sortQuery.set(“dd”, “cc”); sortQuery.set(“cc”, “ccwhy”); // 注意这里覆盖了之前的 “cc”

    <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> query = <span class="hljs-string"><span class="hljs-string">''</span></span>;
    sortQuery.forEach((value, key) =&gt; {
      query += `${<span class="hljs-built_in"><span class="hljs-built_in">encodeURIComponent</span></span>(<span class="hljs-built_in"><span class="hljs-built_in">String</span></span>(key))}=${<span class="hljs-built_in"><span class="hljs-built_in">encodeURIComponent</span></span>(<span class="hljs-built_in"><span class="hljs-built_in">String</span></span>(value))}&amp;`;
    });
    query = query.slice(<span class="hljs-number"><span class="hljs-number">0</span></span>, -<span class="hljs-number"><span class="hljs-number">1</span></span>); <span class="hljs-comment"><span class="hljs-comment">// 删除最后一个多余的 "&amp;"</span></span>

    <span class="hljs-keyword"><span class="hljs-keyword">const</span></span> url = `http:<span class="hljs-comment"><span class="hljs-comment">//xxxx?${query}`;</span></span>
    console.log(`url:${url}`);
  })
}
.height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)

} }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

打印

url:http://xxxx?ab=cc&cc=ccwhy&dd=cc<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

我怀疑是map不能被序列化 我还是老老实实用Record序列化吧

我怀疑是map不能被序列化 我还是老老实实用Record序列化吧

这样?

import { TreeMap } from '@kit.ArkTS';

@Entry @Component struct Page45 {

build() { Column() { Button(‘测试’).onClick(() => { let record1: Record<string, string> = { ‘ab’: ‘cc’, ‘cc’: ‘cc’ }; let record2: Record<string, string> = { ‘dd’: ‘cc’ };

    <span class="hljs-comment"><span class="hljs-comment">// 创建一个新的 TreeMap</span></span>
    <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> sortQuery: TreeMap&lt;string, string&gt; = <span class="hljs-keyword"><span class="hljs-keyword">new</span></span> TreeMap();

    <span class="hljs-comment"><span class="hljs-comment">// 将第一个 Record 添加到 TreeMap 中</span></span>
    <span class="hljs-built_in"><span class="hljs-built_in">Object</span></span>.keys(record1).forEach((key:string)=&gt;{
      sortQuery.set(key, record1[key]);
    })

    <span class="hljs-comment"><span class="hljs-comment">// 将第二个 Record 添加到 TreeMap 中</span></span>
    <span class="hljs-built_in"><span class="hljs-built_in">Object</span></span>.keys(record2).forEach((key:string)=&gt;{
      sortQuery.set(key, record2[key]);
    })

    <span class="hljs-comment"><span class="hljs-comment">// 打印</span></span>
    console.info(<span class="hljs-string"><span class="hljs-string">'ab'</span></span>,sortQuery.get(<span class="hljs-string"><span class="hljs-string">'ab'</span></span>))
    console.info(<span class="hljs-string"><span class="hljs-string">'dd'</span></span>,sortQuery.get(<span class="hljs-string"><span class="hljs-string">'dd'</span></span>))
  })
}
.height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)

} }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

打印

ab cc
dd cc<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

let sortQuery = new TreeMap(); sortQuery.set(“ab”,“cc”); sortQuery.set(“cc”,“cc”); sortQuery.set(“dd”,“cc”); sortQuery.set(“cc”,“ccwhy”);

调用 ${JSON.stringify(sortQuery)} 返回 {} 期望返回排序的 {“ab”:“cc”,“cc”:“cc”,“cc”:“ccwhy”,“dc”:“cc”}
你怀疑为什么有两个cc呢? post请求如下 http://www.baidu.com?cc=cch±/$%-+{“cc”:“ccwhy”} 我们要把params和body合并加密 一个参数都不能少!!!

import { TreeMap } from '@kit.ArkTS';

@Entry @Component struct Page45 {

build() { Column() { Button(‘测试’).onClick(()=>{ let sortQuery: TreeMap<string, string> = new TreeMap(); sortQuery.set(“ab”, “cc”); sortQuery.set(“cc”, “cc”); sortQuery.set(“dd”, “cc”);

    <span class="hljs-comment"><span class="hljs-comment">// 创建一个普通对象来存放 TreeMap 的键值对</span></span>
    <span class="hljs-keyword"><span class="hljs-keyword">let</span></span> jsonifiableObject: Record&lt;string, string&gt; = {};

    <span class="hljs-comment"><span class="hljs-comment">// 遍历 TreeMap 并将键值对存入新对象</span></span>
    <span class="hljs-keyword"><span class="hljs-keyword">for</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> entry of sortQuery.entries()) {
      jsonifiableObject[<span class="hljs-built_in"><span class="hljs-built_in">Object</span></span>(entry)[<span class="hljs-number"><span class="hljs-number">0</span></span>]] = <span class="hljs-built_in"><span class="hljs-built_in">Object</span></span>(entry)[<span class="hljs-number"><span class="hljs-number">1</span></span>];
    }

    <span class="hljs-comment"><span class="hljs-comment">// 使用 JSON.stringify() 序列化新对象</span></span>
    console.log(<span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(jsonifiableObject));
  })
}
.height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)

} }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

打印

{“ab”:“cc”,“cc”:“cc”,“dd”:“cc”}<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

thanks 我试试!!

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17

我是遍历两个Record 到 Treemap里面 现在好像又回去了 我直接合并两个Record行是行 就是不知道 Treemap为什么不行

我是遍历两个Record 到 Treemap里面 现在好像又回去了 我直接合并两个Record行是行 就是不知道 Treemap为什么不行

let sortQuery: TreeMap<string, string> = new TreeMap()

    sortQuery.forEach((value, key) => {

      LogUtils.debug("fast_http_request >>> url", `排序后:Key: ${key}, Value: ${value}`);

    });

    

fast_http_request >>> url, 排序后:Key: address, Value:
fast_http_request >>> url, 排序后:Key: gt, Value: kksjc831541b2e4f58a834a580fbaea3ff
fast_http_request >>> url, 排序后:Key: lat, Value: 59.85936050755619
fast_http_request >>> url, 排序后:Key: lon, Value: 136.3490077263818
fast_http_request >>> url, 排序后:Key: mobile, Value: 2813345251
fast_http_request >>> url, 排序后:Key: userId, Value: 234085
fast_http_request >>> url, 排序后:Key: znId, Value: 234085
LogUtils.debug("fast_http_request >>> url",
`签名配置bodyString: ${JSON.stringify(sortQuery)}`);

签名配置  bodyString: {}

HarmonyOS 鸿蒙Next中,TreeMap等复杂数据结构可能不直接支持JSON.stringify的序列化。这通常是因为TreeMap包含额外的功能和属性,这些在普通JSON对象中不存在。为确保正确序列化,可以将TreeMap转换为普通对象后再进行序列化。如果问题依旧没法解决,请加我微信,我的微信是itying888。这样可以进一步探讨解决方案。

回到顶部