go - Ignore JSON tags when marshalling -
I am getting JSON data from an external source. I do not want to take the field name in this JSON with me, I am converting those names that I understand by using the When I take such an object back to JSON, then I naturally retrieve the ugly (original) names again. Is there a way to ignore the tag during martial arts? Or a way to specify a different name for Marshall and Unmershail? To clarify, I have prepared the same code and pasted it below. Thanks in advance. Package type import ("Encoding / Jason" "FMT") type band structure {name string: json: "bandname" 'album' int` json: "albumcount" `} func main () {/ p> // JSON - & gt; Object data: = [] byte (`{" band name ":" AC / DC "," albumcount ": 10}`) Band: = and band {} Jason. Universal (data, band) // object - & gt; JSON str, _ :: json.marshal (band) fmt.println ("real result:", strings (str) fmt.println ("desired result:", `{" name ":" ac / dc "," "AC / DC", "AC / DC", "AlbumCount": 10} // Expected Result: {"Name": "AC / DC", " Jason: "Original Name" tags.
encoding / jason
package of standard library> type marshaler interface {martial json () ([byte, error)} example :
type band structure {name string: json: "bandname" 'album int` json: "albumcount" `} func (b band) Marshall json ([by T, error] {n, _: = json.marshal (b.Name) a, _ :: json.marshal (b.lbums) back [] byte (`{" "name": `+ string (n) + ' , "Album": `+ string (a) +`} `)}
Although this is not a very good solution.
Comments
Post a Comment