flutter使用websocket长连接,返回的ArrayBuffer如何解析成json

如下图返回的数据

[31, 139, 8, 0, 0, 0, 0, 0, 0, 0, 21, 78, 187, 14, 130, 64, 16, 252, 21, 51, 245, 177, 97, 23, 110, 129, 235, 16, 72, 48, 26, 45, 60, 27, 59, 16, 10, 11, 26, 197, 138, 240, 239, 222, 77, 51, 153, 204, 35, 179, 225, 181, 76, 112, 248, 254, 70, 250, 12, 235, 12, 131, 105, 88, 7, 184, 13, 143, 123, 235, 35, 31, 155, 30, 78, 75, 166, 210, 26, 52, 245, 185, 131, 147, 140, 152, 43, 131, 246, 22, 18, 121, 74, 89, 128, 193, 179, 107, 130, 197, 74, 26, 196, 197, 71, 97, 153, 10, 53, 224, 211, 53, 142, 88, 82, 181, 18, 139, 190, 239, 124, 13, 199, 66, 92, 85, 186, 239, 6, 235, 123, 153, 195, 15, 73, 133, 147, 52, 79, 152, 15, 34, 193, 119, 89, 129, 253, 15, 73, 66, 141, 178, 165, 0, 0, 0]
Ricardo
讨论数量: 2

main() { Uint8List input = Uint8List.fromList([ 255, 20, 0, 11, 0, 0, 0, 15, 80, 82, 69, 77, 84, 65, 72, 73, 76, 45, 53, 53, 57, 55, 48 ]);

ByteData bd = input.buffer.asByteData(); print(bd.getUint16(1, Endian.little)); // print the first short print(bd.getUint16(3, Endian.little)); // and the second print(bd.getUint16(5, Endian.little)); // and the third int stringLength = input[7]; // get the length of the string print(utf8.decode(input.sublist(8, 8 + stringLength))); // decode the string }

2年前 评论

格式化了一下楼上的代码:

main() {
 Uint8List input = Uint8List.fromList([ 255, 20, 0, 11, 0, 0, 0, 15, 80, 82, 69, 77, 84, 65, 72, 73, 76, 45, 53, 53, 57, 55, 48 ]);

ByteData bd = input.buffer.asByteData();
 print(bd.getUint16(1, Endian.little));
 // print the first short print(bd.getUint16(3, Endian.little));
 // and the second print(bd.getUint16(5, Endian.little));
 // and the third int stringLength = input[7];
 // get the length of the string
 print(utf8.decode(input.sublist(8, 8 + stringLength))); // decode the string
 }
2年前 评论
u100 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!