有Json标记,但未导出[重复]

This question already has an answer here:

Begining to study golang. Task: Get Json and Unmarshall it. But I get mistake:

Json tag but not exported

How to make unexported fields become exported and then implement it using methods?

Here is the code:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type Time struct {
    time
}
type time struct {
    id                    string  `json:"$id"`
    currentDateTime       string  `json:"currentDateTime,string"`
    utcOffset             float64 `json:"utcOffset,string"`
    isDayLightSavingsTime bool    `json:"isDayLightSavingsTime,string"`
    dayOfTheWeek          string  `json:"dayOfTheWeek,string"`
    timeZoneName          string  `json:"timeZoneName,string"`
    currentFileTime       float64 `json:"currentFileTime,string"`
    ordinalDate           string  `json:"ordinalDate,string"`
    serviceResponse       string  `json:"serviceResponse,string"`
}

func (t *Time) GetTime() (Time, error) {
    result := Time{}

    return result, t.Timenow(result)
}
func (t *Time) Timenow(result interface{}) error {

    res, err := http.Get("http://worldclockapi.com/api/json/utc/now")
    if err != nil {
        fmt.Println("Cannot get Json", err)
    }

    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        fmt.Println("Cannot create Body", err)
    }

    defer res.Body.Close()

    var resultJson interface{}
    return json.Unmarshal(body, &resultJson)

}

func main() {

    var a Time
    t, err := a.GetTime()
    if err != nil {
        fmt.Println("Error ", err)
    }
    fmt.Println("Time:", t)
}

Please explain in details whats wrong with struct and how to get right response?

</div>
douxin8383
douxin8383 encoding/json文档说明必须导出字段。该规范描述了导出的标识符。
2 年多之前 回复
drau67562
drau67562 golang.org/ref/spec#Exported_identifiers
2 年多之前 回复
dtc99987
dtc99987 您的结构值需要通过使用大写的字段名称来导出。例如,您需要使用CurrentDateTime而不是currentDateTime。
2 年多之前 回复

1个回答



您正在向未导出的字段添加JSON标签。</ p>

结构 字段必须以大写字母开头(已导出),以查看JSON包的值。< / p>

  struct struct {
// JSON包不可见未导出的struct字段。
//通过以大写字母开头的字段来导出字段。
未导出的字符串

// {“已导出”:“”}
导出的字符串

// {“ custom_name”:“”}
CustomName字符串json:“ custom_name”
}
</ code> </ pre>

此要求的根本原因是JSON包使用 reflect </ code>检查结构字段。 由于 reflect </ code>不允许访问未导出的struct字段,因此JSON包看不到它们的值。</ p>
</ div>

展开原文

原文

You're adding a JSON tag to a field that isn't exported.

Struct fields must start with upper case letter (exported) for the JSON package to see their value.

struct A struct {
    // Unexported struct fields are invisible to the JSON package.
    // Export a field by starting it with an uppercase letter.
    unexported string

    // {"Exported": ""}
    Exported string

    // {"custom_name": ""}
    CustomName string `json:"custom_name"`
}

The underlying reason for this requirement is that the JSON package uses reflect to inspect struct fields. Since reflect doesn't allow access to unexported struct fields, the JSON package can't see their value.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐