直接使用json.RawMessage的打印功能

直接使用json.RawMessage的打印功能

问题描述:

I'm doing some work with Elasticsearch and the query return a Source object which it's type is *json.RawMessage.

I only want to print it to screen without creating struct model for it and doing the obvious json.Marshal.

Is there a print function that will consume *json.RawMessage type and print it to screen?

Code sample:

 for _, hit := range serachResult.Hits.Hits {
    fmt.Println(hit.Source, "
")
 }

This code run will result in un-readable array of bytes, apparently without the ability to just build a string from the raw message.

我正在使用 Elasticsearch code>做一些工作,查询返回一个 Source code>对象的类型是 * json.RawMessage code>。 p>

我只想将其打印到屏幕上,而无需为其创建结构模型并做明显的事情 json.Marshal code>。 p>

是否存在将使用* json.RawMessage类型并将其打印到屏幕的打印功能? p> 代码示例: p>

 表示_,命中==范围serachResult.Hits.Hits {
 fmt.Println(hit.Source,“ 
”)
}  
  code>  pre> 
 
 

此代码运行将导致无法读取的字节数组,显然无法从原始消息中构建 string code> p> div>

You can use %s to printf:

for _, hit := range serachResult.Hits.Hits {
   fmt.Printf("%s
", hit.Source)
}