duanchu3376 2016-06-06 13:26
浏览 211
已采纳

直接使用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.

  • 写回答

1条回答 默认 最新

  • dongmu3187 2016-06-06 13:31
    关注

    You can use %s to printf:

    for _, hit := range serachResult.Hits.Hits {
       fmt.Printf("%s
    ", hit.Source)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?