登录
首页 >  Golang >  Go问答

将 Golang 中的字节数组转换为 JSON 格式的节点

来源:stackoverflow

时间:2024-02-12 09:09:24 122浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《将 Golang 中的字节数组转换为 JSON 格式的节点》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

嗨,我有两个服务,一个在 golang 中,另一个在 nodejs 中

golang one 使用以下代码生成到 kafka

var network bytes.buffer        
enc := gob.newencoder(&network)
enc.encode(product)
produce_to_kafka("add", network.bytes())

节点正在消耗以下内容

await consumer.run({
  eachmessage: async ({ topic, partition, message, heartbeat }) => {
      console.log({
          key: message.key.tostring(),
          value: new buffer.from(message.value, "binary").tostring('ascii'),
          headers: message.headers,
      })
  },
})

如您所见,我尝试使用 buffer.from 与 ascii、二进制和 base64 进行转换,但没有成功。

这是我在nodejs中得到的console.log

[categories]   key: 'add',
[categories]   value: '\x7fo\x7f\x01\x03\x01\x01\x07product\x01\x7f\x02\x00\x01\x0f\x01\x04name\x01\f\x00\x01\x02id\x01\f\x00\x01\x07storeid\x01\f\x00\x01\tstoreprod\x01\f\x00\x01\x05price\x01\b\x00\x01\bdiscount\x01\b\x00\x01\x05brand\x01\f\x00\x01\n' +
[categories]     'categories\x01\x7f\x04\x00\x01\x04tags\x01\x7f\x04\x00\x01\blocation\x01\f\x00\x01\x07instock\x01\x04\x00\x01\n' +
[categories]     'expirydate\x01\f\x00\x01\ttotalsold\x01\x04\x00\x01\x11laststockaddition\x01\f\x00\x01\x04cost\x01\x04\x00\x00\x00\x16\x7f\x03\x02\x01\x01\b[]string\x01\x7f\x04\x00\x01\f\x00\x00i\x7f\x02\x01\x04stng\x01\x05stngp\x01\x01t\x01\x06tstngp\x01@\x01@\x01\x06string\x01\x01\x05askdl\x01\x01\x05asdas\x01\x06string\x01\x04\x01\x102021-10-03 12:13\x01\x06\x01\x102021-10-03 12:13\x01\b\x00',
[categories]   headers: {}
[categories] }

此外,我在 golang 中对同一主题有一个消耗,并且可以使用以下代码成功地将其解析回结构

var network bytes.Buffer;
network.WriteString(string(ev.Value))
dec := gob.NewDecoder(&network) 
var q model.Product
err = dec.Decode(&q)

您能否分享任何资源或语法,我如何在 nodejs 中执行此操作


正确答案


您可以使用 nodejs Buffer API 解码二进制编码字符串。

所以在你的情况下,它应该给出这样的内容:

var binaryString = '\x7FO\x7F\x01\x03\x01\x01\x07Product\x01\x7F\x02\x00\x01\x0F\x01\x04Name\x01\f\x00\x01\x02Id\x01\f\x00\x01\x07Storeid\x01\f\x00\x01\tStoreProd\x01\f\x00\x01\x05Price\x01\b\x00\x01\bDiscount\x01\b\x00\x01\x05Brand\x01\f\x00\x01\nCategories\x01\x7F\x04\x00\x01\x04Tags\x01\x7F\x04\x00\x01\bLocation\x01\f\x00\x01\x07InStock\x01\x04\x00\x01\nExpiryDate\x01\f\x00\x01\tTotalSold\x01\x04\x00\x01\x11LastStockAddition\x01\f\x00\x01\x04Cost\x01\x04\x00\x00\x00\x16\x7F\x03\x02\x01\x01\b[]string\x01\x7F\x04\x00\x01\f\x00\x00i\x7F\x02\x01\x04Stng\x01\x05Stngp\x01\x01t\x01\x06tStngp\x01@\x01@\x01\x06String\x01\x01\x05askdl\x01\x01\x05asdas\x01\x06String\x01\x04\x01\x102021-10-03 12:13\x01\x06\x01\x102021-10-03 12:13\x01\b\x00';

const data = Buffer.from(binaryString, "binary");

console.log(data);
console.log(data.toString());

但是,正如评论中提到的,我不知道 go gob 编码器和 nodejs 二进制解码器是否在所有情况下都能正常工作。也许使用其他格式可能会更好。

希望它能有所帮助。

好了,本文到此结束,带大家了解了《将 Golang 中的字节数组转换为 JSON 格式的节点》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>