gpt4 book ai didi

javascript - 如何将 JavaScript 对象 (JSON) 转换为 JSV 格式?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:24:08 24 4
gpt4 key购买 nike

在将 ajax 请求发送到 ServiceStack 服务时,我希望使用 JSV 而不是 JSON 来节省带宽。

我有以下 JSON 数据:

[{"201":"New York","022":"Chicago"}]

我想将其转换为以下 JSV 格式:

[{201:New York,022:Chicago}]

有没有办法简单地做到这一点?我不确定处理它的最佳方法是什么;但根据我的知识/理解,我需要使用 : (冒号)和 , (逗号)拆分字符串,并且需要循环遍历它。但我担心这不能解释字符转义。是否有可用于解析 JSV 格式的官方 JavaScript 库 - 我找不到?

最佳答案

Is there any advantages on JSV over JSON other than few bytes saved in JSV format?

JSV 可用于在 GET 请求中发送复杂类型,而使用 JSON 则无法做到这一点。 Some of these other answers ,展示了 JSV 在 GET 请求中发送复杂类型的用法。

Do we have any JavaScript library or function to parse JSV?

JavaScript 的官方 JSV 库 can be found here .它还包括一个用 JavaScript 实现的 JsvServiceClient

I am not sure what is the best way to process it but as per my knowledge/understanding we need to split the string using : (colon) and , (comma) and need to loop through it.

使用上面的库,避免自己解析。

反序列化用法:

JSV.parse("<JSV String>");

序列化用法:

var myObject = {Id: 123, Test: [1,2,3]};
JSV.stringify(myObject);

I am planning to use JSV instead of JSON format to save the bandwidth

除非您计划使用特别大的数据集,否则使用 JSV 的节省可能会因发送 JavaScript JSV 解析库来处理请求的要求而被抵消。官方 JSV 库,缩小后 15KB 或缩小后约 8KB。请记住,JSON 支持已经内置到 Web 浏览器中,无需额外开销。

关于javascript - 如何将 JavaScript 对象 (JSON) 转换为 JSV 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25645046/

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