SoFunction
Updated on 2025-03-04

The reason and solution of the error of using Unmarshal to assign json to struct

For example:

Convert json:

{
 "name": "Laura"
 "age": "18"
}

Assign to struct:

type PersonalInfo struct {
 Name string `json:"name"`
 Age string `json:"age"`
}

Use statement:

person := PersonalInfo{}
err := (json, &persona)//jsonFor the above[]byte

Cause of error:

1. The variable name in struct is not exportable (the first letter is lowercase), and the first letter needs to be changed to uppercase

2. Need to transmit the pointer to the person

3. The name of json in struct needs to be exactly the same as that in json

Supplement: Go language processing JSON - using Unmarshal to parse json strings

Simple analysis example:

First of all, from the official documentation example:

package main
import (
 "fmt"
 "encoding/json"
)
type Animal struct {
 Name string
 Order string
}
func main() {
 var jsonBlob = []byte(`[
 {"Name": "Platypus", "Order": "Monotremata"},
 {"Name": "Quoll", "Order": "Dasyuromorphia"}
 ]`)
 var animals []Animal
 
 err := (jsonBlob, &animals)
 if err != nil {
  ("error:", err)
 }
 ("%+v", animals)
}

Output:

[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]

Make simple modifications, modifying them to:

package main
import (
 "fmt"
 "encoding/json"
)
type Animal struct {
 Name string
 Order string
}
func main() {
 var jsonBlob = []byte(`{"Name": "Platypus", "Order": "Monotremata"}`)
 var animals Animal
 err := (jsonBlob, &animals)
 if err != nil {
  ("error:", err)
 }
 ("%+v", animals)
}

Output:

{Name:Platypus Order:Monotremata}

Still the previous example:

Parses such a json string:

{
 "first fruit":
 {
  "describe":"an apple",
  "icon":"appleIcon",
  "name":"apple"
 },
 "second fruit":
 {
  "describe":"an orange",
  "icon":"orangeIcon",
  "name":"orange"
 },
 "three fruit array":
 [
  "eat 0",
  "eat 1",
  "eat 2",
  "eat 3",
  "eat 4"
 ]
}

go code:

package main
import (
 "fmt"
 "encoding/json"
)
type Fruit struct {
 Describe string `json:"describe"`
 Icon  string `json:"icon"`
 Name  string `json:"name"`
}
type FruitGroup struct {
 FirstFruit *Fruit `json:"first fruit"` //Pointer, pointing to the reference object; if you do not use a pointer, just copy the value SecondFruit *Fruit `json:"second fruit"` //Pointer, pointing to the reference object; if you do not use a pointer, just copy the value THreeFruitArray []string `json:"three fruit array"`
}
func main() {
 var jsonBlob = []byte(`{
 "first fruit": {
  "describe": "an apple",
  "icon": "appleIcon",
  "name": "apple"
 },
 "second fruit": {
  "describe": "an orange",
  "icon": "appleIcon",
  "name": "orange"
 },
 "three fruit array": [
  "eat 0",
  "eat 1",
  "eat 2",
  "eat 3"
 ]}`)
 var fruitGroup FruitGroup
 
 err := (jsonBlob, &fruitGroup)
 if err != nil {
  ("error:", err)
 }
 ("%+v\n", fruitGroup)
 ("%+v\n", )
 ("%+v\n", )
}

Running results:

{FirstFruit:0xc00006c5a0 SecondFruit:0xc00006c5d0 THreeFruitArray:[eat 0 eat 1 eat 2 eat 3]}
&{Describe:an apple Icon:appleIcon Name:apple}
&{Describe:an orange Icon:appleIcon Name:orange}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.