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

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

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

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:

确定