如何简单地打印原始的 JSON 字符串?

huangapple go评论184阅读模式
英文:

How do I simply print a raw json string?

问题

我正在尝试获取这个混乱数据的底层数据。这是一个特定于GKE的PubSub消息,我不知道底层的JSON是什么样子的。JSON中还有一个attributes键,这是我最感兴趣的部分。有没有办法可以将传入的JSON转储出来,以便我可以看到它的样子?

  1. // Package p contains a Pub/Sub Cloud Function.
  2. package p
  3. import (
  4. "context"
  5. "log"
  6. )
  7. // PubSubMessage is the payload of a Pub/Sub event. Please refer to the docs for
  8. // additional information regarding Pub/Sub events.
  9. type PubSubMessage struct {
  10. Data []byte `json:"data"`
  11. }
  12. // HelloPubSub consumes a Pub/Sub message.
  13. func HelloPubSub(ctx context.Context, m PubSubMessage) error {
  14. log.Println(string(m.Data))
  15. return nil
  16. }
英文:

I'm trying to get at the underlying data for this mess. It's a GKE-specific PubSub message and I don't know what the underlying JSON looks like. There's also an attributes key in the json and that's what I'm most interested in. Is there a way I can just dump the incoming json so I can see what it looks like?

  1. // Package p contains a Pub/Sub Cloud Function.
  2. package p
  3. import (
  4. "context"
  5. "log"
  6. )
  7. // PubSubMessage is the payload of a Pub/Sub event. Please refer to the docs for
  8. // additional information regarding Pub/Sub events.
  9. type PubSubMessage struct {
  10. Data []byte `json:"data"`
  11. }
  12. // HelloPubSub consumes a Pub/Sub message.
  13. func HelloPubSub(ctx context.Context, m PubSubMessage) error {
  14. log.Println(string(m.Data))
  15. return nil
  16. }

答案1

得分: 1

  1. json.NewEncoder(os.Stdout).Encode(&m)

应该将其编码为 JSON 并写入标准输出。

英文:
  1. json.NewEncoder(os.Stdout).Encode(&m)

should encode as json and write to stdout

huangapple
  • 本文由 发表于 2021年11月11日 04:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/69919965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定