英文:
How do I simply print a raw json string?
问题
我正在尝试获取这个混乱数据的底层数据。这是一个特定于GKE的PubSub消息,我不知道底层的JSON是什么样子的。JSON中还有一个attributes键,这是我最感兴趣的部分。有没有办法可以将传入的JSON转储出来,以便我可以看到它的样子?
// Package p contains a Pub/Sub Cloud Function.
package p
import (
"context"
"log"
)
// PubSubMessage is the payload of a Pub/Sub event. Please refer to the docs for
// additional information regarding Pub/Sub events.
type PubSubMessage struct {
Data []byte `json:"data"`
}
// HelloPubSub consumes a Pub/Sub message.
func HelloPubSub(ctx context.Context, m PubSubMessage) error {
log.Println(string(m.Data))
return nil
}
英文:
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?
// Package p contains a Pub/Sub Cloud Function.
package p
import (
"context"
"log"
)
// PubSubMessage is the payload of a Pub/Sub event. Please refer to the docs for
// additional information regarding Pub/Sub events.
type PubSubMessage struct {
Data []byte `json:"data"`
}
// HelloPubSub consumes a Pub/Sub message.
func HelloPubSub(ctx context.Context, m PubSubMessage) error {
log.Println(string(m.Data))
return nil
}
答案1
得分: 1
json.NewEncoder(os.Stdout).Encode(&m)
应该将其编码为 JSON 并写入标准输出。
英文:
json.NewEncoder(os.Stdout).Encode(&m)
should encode as json and write to stdout
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论