简体   繁体   English

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

[英]How can I convert a JavaScript object(JSON) into JSV format?

I am looking to use JSV rather than JSON to save bandwidth when sending my ajax requests to my ServiceStack service. 我希望在向我的ServiceStack服务发送ajax请求时使用JSV而不是JSON来节省带宽。

I have the following JSON data: 我有以下JSON数据:

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

And I would like to convert it to the following JSV format: 我想将其转换为以下JSV格式:

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

Is there a way to do this simply? 有办法简单地做到这一点吗? I am not sure what is the best way to process it; 我不确定处理它的最佳方法是什么; But as per my knowledge/understanding I need to split the string using : (colon) and , (comma) and need to loop through it. 但根据我的知识/理解,我需要使用:冒号)和(逗号)拆分字符串,并需要循环它。 But I am concerned this doesn't account for character escaping. 但我担心这不会导致角色逃脱。 Is there an official JavaScript library that can be used to parse the JSV format - I couldn't find one? 是否有可用于解析JSV格式的官方JavaScript库 - 我找不到一个?

Is there any advantages on JSV over JSON other than few bytes saved in JSV format? 除了JSV格式保存的少量字节之外,JSV优于JSON有什么优势吗?

JSV can be used to send complex types in a GET request, which isn't possible using JSON. JSV可用于在GET请求中发送复杂类型,这是使用JSON无法实现的。 Some of these other answers , show the use of JSV to send complex types in a GET request. 其中一些答案显示了使用JSV在GET请求中发送复杂类型。

Do we have any JavaScript library or function to parse JSV? 我们有解析JSV的JavaScript库或函数吗?

The official JSV library for JavaScript can be found here . 可以在此处找到适用于JavaScript的官方JSV库。 It also includes a JsvServiceClient implemented in JavaScript. 它还包括一个用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. 我不确定处理它的最佳方法是什么,但根据我的知识/理解,我们需要使用:(冒号)和(逗号)拆分字符串,并需要循环它。

Use the above library and avoid parsing it yourself. 使用上面的库,避免自己解析它。

Deserialize Usage: 反序列化用法:

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

Serialize Usage: 序列化用法:

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而不是JSON格式来节省带宽

Unless you are planning on using exceptionally large data sets, then the saving from using JSV may be negated by the requirement to send a JavaScript JSV parsing library to handle the requests. 除非您计划使用异常大的数据集,否则可以通过发送JavaScript JSV解析库来处理请求来抵消使用JSV的节省。 The official JSV library, is 15KB minified or about 8KB minified. 官方JSV库,缩小了15KB或缩小了大约8KB。 Remember that JSON support is already built in to web browsers without overhead. 请记住,JSON支持已经内置到Web浏览器中而没有开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM