简体   繁体   English

如何在Javascript中转换JSON字节

[英]How to convert JSON Bytes in Javascript

I have inherited a somewhat large, Java- based server- side codebase that communicates with clients using JSON. 我继承了一个很大的基于Java的服务器端代码库,该代码库使用JSON与客户端进行通信。 For several reasons, the Jackson- based JSON conversions must use the binary form -- in other words, the JSON that I am generating on the server side is made up of bytes, not characters. 由于多种原因,基于杰克逊的JSON转换必须使用二进制形式-换句话说,我在服务器端生成的JSON由字节而不是字符组成。

This was not a problem with previous clients, because they were all Java based. 对于以前的客户端,这不是问题,因为它们都是基于Java的。 I could use the Jackson libraries to convert the binary back into objects. 我可以使用Jackson库将二进制文件转换回对象。 Unfortunately, I have been asked to create some browser- based clients that require the use of Javascript in order to receive and display the JSON information. 不幸的是,有人要求我创建一些基于浏览器的客户端,这些客户端需要使用Javascript才能接收和显示JSON信息。

I am attempting to use JQuery's JSON conversion functions, but it turns out that neither JQuery nor the regular Javascript functions are capable of converting a set of JSON bytes back into an object. 我正在尝试使用JQuery的JSON转换函数,但事实证明,JQuery和常规Javascript函数都无法将一组JSON字节转换回对象。 I need to convert the bytes back into characters in order to convert them back into objects. 我需要将字节转换回字符,以便将它们转换回对象。

I learned that in order to convert bytes into characters, I have to get the bytes into a byte array. 我了解到,为了将字节转换为字符,我必须将字节转换为字节数组。 This is where I am having the problems. 这就是我遇到的问题。

I have been trying to use various methods to convert the binary JSON information back into a form (a byte array) that can be converted into a meaningful object using the JSON conversion functions. 我一直在尝试使用各种方法将二进制JSON信息转换回可以使用JSON转换功能转换为有意义的对象的形式(字节数组)。 I have tried using the ArrayBuffer to convert the data into a byte buffer, then the int8Array() function to create a byte array: 我尝试过使用ArrayBuffer将数据转换为字节缓冲区,然后使用int8Array()函数创建字节数组:

var buffer = new ArrayBuffer(obj.payload.length);
var bits =  new Int8Array(buffer);

I also attempted to use uint8Array(), and tried various other conversion routines found on the Internet (and here in Stack Overflow) in order to create the array that can be converted into meaningful characters. 我还尝试使用uint8Array(),并尝试了Internet(以及此处的Stack Overflow)中找到的各种其他转换例程,以创建可以转换为有意义的字符的数组。 All my attempts have either caused errors and failures at runtime, or have generated garbage that breaks the JSON converters. 我的所有尝试要么在运行时导致了错误和故障,要么生成了破坏JSON转换器的垃圾。

Is there some way to get Javascript to read a set of bytes generated by Jackson so that they can be converted back to objects using the JSON converters? 是否可以通过某种方式使Javascript读取Jackson生成的一组字节,以便可以使用JSON转换器将其转换回对象? Or is Javascript only capable of converting JSON strings? 还是Javascript仅能够转换JSON字符串?

Someone please advise... 有人请指教...

Assume your binary JSON is something like this: 假设您的二进制JSON是这样的:

[123, 34, 107, 101, 121, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125]

you could easily convert it to a string: 您可以轻松地将其转换为字符串:

var str = String.fromCharCode.apply(String, data);

and parse that with the JSON objects parse: 并使用JSON对象进行解析:

JSON.parse(str);

jsfiddle 的jsfiddle

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

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