liuxuzxx commented on issue #3369:
URL: https://github.com/apache/rocketmq/issues/3369#issuecomment-964736243
在Go当中找到了一种解决方案,总体的解决思路,就是使用JS来做个中转处理:
1. 引入如下的Go的JS引擎执行依赖
```shell
go get github.com/robertkrimen/otto
```
2. 调用JS进行序列化处理
```go
// response是获取的Cluster Information不规范的JSON结果
func BuildClusterInformation(response []byte) ClusterInformation {
vm := otto.New()
vm.Set("source", string(response))
value, err := vm.Run(`
var code = 'JSON.stringify(' + source + ')';
//拼凑字符串:JSON.stringify(source)
eval(code);//通过eval函数执行
`)
if err != nil {
log.Fatal(err.Error())
}
result, _ := value.ToString()//获取执行的结果
}
```
3. 结果比对
```json
//没有转换之前的结果:
{"brokerAddrTable":{"broker_37_master":{"brokerAddrs":{0:"172.16.16.37:10911"},"brokerName":"broker_37_master","cluster":"37_cluster"}},"clusterAddrTable":{"37_cluster":["broker_37_master"]}}
//转换之后的结果:
{"brokerAddrTable":{"broker_37_master":{"brokerAddrs":{"0":"172.16.16.37:10911"},"brokerName":"broker_37_master","cluster":"37_cluster"}},"clusterAddrTable":{"37_cluster":["broker_37_master"]}}
```
可以看到 0 这个信息已经携带上了双引号,变成了 "0"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]