drb88830 2016-04-01 06:51
浏览 70
已采纳

在使用bitly的go-simplejson时,在simplejson.Json文字中显示未导出字段'data'的隐式分配

When I use like &simplejson.Json{v} (v is a interface read from file and it's actual data structure is map[string]interface{}), then show this error. Details:

A json file named abcd

{
    "pids": [
        { 
            "pid": 168043, 
            "target_regions": [
                40, 
                25, 
                43, 
                299, 
                240
            ]
         },
        { 
            "pid": 168044, 
            "target_regions": [
                63, 
                65,
                68
            ]
        }
    ]

}

And the go file is

package main    

import (
    "fmt"
    "io/ioutil"    

    sjson "github.com/bitly/go-simplejson"
)    

type pidInfo struct {
    Pid           uint64   `json:"pid"`
    TargetRegions []uint32 `json:"target_regions"`
}    

type pidUnitInfo struct {
    Pid2Info map[uint64]*pidInfo
}    

func build() error {
    content, _ := ioutil.ReadFile("./abcd")
    json, err := sjson.NewJson(content)
    if err != nil {
        return err
    }
    newPidUnitInfo(json)
    return nil
}    

func newPidUnitInfo(json *sjson.Json) (*pidUnitInfo, error) {
    newInfo := new(pidUnitInfo)
    newInfo.buildPid2Info(json)
    return nil, nil
}    

func (pui *pidUnitInfo) buildPid2Info(json *sjson.Json) error {
    raw, ok := json.CheckGet("pids")
    if !ok {
        return fmt.Errorf("not found json key %v", "pids")
    }
    pids, err := raw.Array()
    if err != nil {
        return err
    }
    pui.Pid2Info = make(map[uint64]*pidInfo, len(pids))
    for _, v := range pids {
        fmt.Println(v)
        m := &sjson.Json{v}
        fmt.Println(m)
    }
    return nil
}    

func main() {
    build()
}

When I execute it, show implicit assignment of unexported field 'data' in simplejson.Json literal at this line m := &sjson.Json{v}.

  • 写回答

1条回答 默认 最新

  • dongpiao0731 2016-04-01 06:56
    关注

    This line:

    m := &sjson.Json{v}
    

    Tries to create a value (and take the address) of the struct type Json from package go-simplejson. The type declaration is:

    type Json struct {
        data interface{}
    }
    

    It has one field: data which is unexported. That means packages other than go-simplejson cannot refer to this field. When you use a struct literal &sjson.Json{v}, it would try to initialize the Json.data field with value v which is a violation of this. You cannot do this.

    The Json type is not designed for you to specify the internal data, it is designed so that the data will be the placeholder of some decoded JSON data (see the NewFromReader() and NewJson() constructor-like functions).

    This data field is handled internally by the go-simplejson package, you cannot set it yourself. You may use sjson.New() to obtain a new *Json value which will initialize this data field with an empty map (map[string]interface{}). You may also use Json.Map() method which asserts that data is a map and returns it like that:

    js := sjson.New()
    m, err := js.Map()
    if err != nil {
        // Handle error
    }
    
    // Now you have a map of type map[string]interface{}
    

    Or to populate the data inside a Json, you can use its Json.Set() method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?