这是我的代码:
fmt.Println("before Marshalling: ", res4)
warehouseData, _ := json.Marshal(res4)
fmt.Println("after Marshalling", string(warehouseData))
此处输出:
before Marshalling: [map[cellidx:1A13 description:CONN FFC BOTTOM 24POS 0.50MM R/A footprint:<nil> temperature:<nil> help_url: part_number:SFV24R-1STE1HLF ref_supplier:<nil> qty:10 supplier_link:http://www.digikey.com/product-detail/en/amphenol-fci/SFV24R-1STE1HLF/609-4320-1-ND/2626771 notes:<nil>]]
after Marshalling [{"cellidx":"1A13","description":"CONN FFC BOTTOM 24POS 0.50MM R/A","footprint":null,"help_url":"","notes":null,"part_number":"SFV24R-1STE1HLF","qty":10,"ref_supplier":null,"supplier_link":"http://www.digikey.com/product-detail/en/amphenol-fci/SFV24R-1STE1HLF/609-4320-1-ND/2626771","temperature":null}]
问题是res4
变量的类型为[]map[string]interface{}
。例如。该数据是从Postgresql请求中获得的,结构未知。
如果由于数据结构未知而无法使用omitempty
技巧,如何从结果JSON中忽略空字符串?