golang json无法正确解析带有嵌入式结构标签的字段[关闭]

package main

import (
    "encoding/json"
    "fmt"
)

type InnerData struct {
    M int64 `josn:"m"`
    N int64 `json:"n"`
}

//JSONData is a json data example
type JSONData struct {
    Hello string    `json:"hello"`
    Data  InnerData `json:"data"`
}

func main() {
    v := JSONData{Hello: "world", Data: InnerData{N: 100000, M: 123456}}
    mashaled, err := json.Marshal(&v)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(mashaled))
}

Noted the field M in InnerData has a tag m, so the expected result is :{"hello":"world","data":{"m":123456,"n":100000}}. While I have

{"hello":"world","data":{"M":123456,"n":100000}}

Does anyone know how to fix the problem, or where am I wrong?

You have typos in your code:

In your InnerData, you put josn instead of json.

Fix those typos and try again.

Silly mistake in the tag declaration with the spelling of json as josn

type InnerData struct {
    M int64 `josn:"m"` // the spelling is not correct for json.
    N int64 `json:"n"`
}

Change the tag for field M as

type InnerData struct {
    M int64 `json:"m"` // the spelling is not correct for json.
    N int64 `json:"n"`
}

One more thing is InnerData is not embedded struct. In Golang spec embedded struct is described as:

A field declared with a type but no explicit field name is called an embedded field. An embedded field must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

// A struct with four embedded fields of types T1, *T2, P.T3 and *P.T4

struct {
    T1        // field name is T1
    *T2       // field name is T2
    P.T3      // field name is T3
    *P.T4     // field name is T4
    x, y int  // field names are x and y
}