Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(un)marshalling json golang not working

Tags:

json

go

I'm playing with Go and am stumped as to why json encode and decode don't work for me

I think i copied the examples almost verbatim, but the output says both marshal and unmarshal return no data. They also don't give an error.

can anyone hint to where i'm going wrong?

my sample code: Go playground

package main  import "fmt" import  "encoding/json"  type testStruct struct {     clip string `json:"clip"` }  func main() { //unmarshal test     var testJson = "{\"clip\":\"test\"}"     var t testStruct     var jsonData = []byte(testJson)     err := json.Unmarshal(jsonData, &t)     if err != nil {         fmt.Printf("There was an error decoding the json. err = %s", err)         return     }     fmt.Printf("contents of decoded json is: %#v\r\n", t)  //marshal test     t.clip = "test2"     data, err := json.Marshal(&t)     if err != nil {          fmt.Printf("There was an error encoding the json. err = %s", err)          return     }     fmt.Printf("encoded json = %s\r\n", string(data)) } 

output:

 contents of decoded json is: main.testStruct{clip:""}  encoded json = {} 

in both outputs I would have expected to see the decoded or encoded json

like image 483
Toad Avatar asked Aug 31 '14 18:08

Toad


People also ask

What does Omitempty do in Golang?

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

What is JSON marshalling in Golang?

What is marshaling in Go? Encoding Go objects to JSON format is known as marshaling. We can use the Marshal function to convert Go objects to JSON. The Marshal function comes with the following syntax. func Marshal(v interface{}) ([]byte, error)

How does Unmarshal work in Golang?

Golang Unmarshal Unmarshal is the contrary of marshal. It allows you to convert byte data into the original data structure. In go, unmarshaling is handled by the json. Unmarshal() method.

How does JSON Unmarshal work Golang?

According to Golang spec Unmarshal takes data in bytes format and interface{} to wrap the variable storing the data at an address. Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.


1 Answers

For example,

package main  import "fmt" import "encoding/json"  type testStruct struct {     Clip string `json:"clip"` }  func main() {     //unmarshal test     var testJson = "{\"clip\":\"test\"}"     var t testStruct     var jsonData = []byte(testJson)     err := json.Unmarshal(jsonData, &t)     if err != nil {         fmt.Printf("There was an error decoding the json. err = %s", err)         return     }     fmt.Printf("contents of decoded json is: %#v\r\n", t)      //marshal test     t.Clip = "test2"     data, err := json.Marshal(&t)     if err != nil {         fmt.Printf("There was an error encoding the json. err = %s", err)         return     }     fmt.Printf("encoded json = %s\r\n", string(data)) } 

Output:

contents of decoded json is: main.testStruct{Clip:"test"} encoded json = {"clip":"test2"} 

Playground:

http://play.golang.org/p/3XaVougMTE

Export the struct fields.

type testStruct struct {     Clip string `json:"clip"` } 

Exported identifiers

An identifier may be exported to permit access to it from another package. An identifier is exported if both:

  • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
  • the identifier is declared in the package block or it is a field name or method name.

All other identifiers are not exported.

like image 143
peterSO Avatar answered Sep 29 '22 02:09

peterSO