duancong6937 2013-06-10 14:32
浏览 116

如何插入多维数组

want to save the data of the following format

{"_ibj_id":"1","url_id":'1',"url":{"0":"http://0.com","1":"http:://1.com"}}

Look at my code,

type db_list struct {
    Url_id int
    Url    map[int]string
}
func list(table *mgo.Collection) {

    var doc *goquery.Document

    var e error

    for i := 1628644; i > 1628643; i-- {

        if doc, e = goquery.NewDocument("http://www.120ask.com/list/all/" + strconv.Itoa(i)); e != nil {

            panic(e.Error())

        }

        var save_list db_list

        save_list.Url_id = i

        save_list.Url = make(map[int]string)

        //fmt.Println("%s", doc.Text())

        doc.Find(".q-quename").Each(func(n int, s *goquery.Selection) {

            href, isTrue := s.Attr("href")

            if isTrue {

                save_list.Url[n] = href

                fmt.Println("%D : %s", n, save_list.Url[n])

            }

        })

        fmt.Println("%D", len(save_list.Url))

        //save database

        table.Insert(save_list)

    }

}

The database will eventually save Please view the picture in the annex, is to save the format of the data, save the URLvalue of the property 1enter image description here

  • 写回答

2条回答 默认 最新

  • dongyi8795 2013-06-11 02:53
    关注

    You're probably after the JSON Unmarshal function in encoding/json

    {"_ibj_id":"1","url_id":'1',"url":{"0":"http://0.com","1":"http:://1.com"}} is technically invalid JSON due to the single-quotes around the url_id value( '1' should be "1") but other than that, it should map nicely to the following struct:

    {
    id string
    url_id string
    urls []string
    }
    

    But you may need to experiment with the types. According to the docs for the Unmarshal function, it will use the following Go types for each JSON type:

    bool, for JSON booleans
    float64, for JSON numbers
    string, for JSON strings
    []interface{}, for JSON arrays
    map[string]interface{}, for JSON objects
    nil for JSON null
    

    I'd highly recommend reading Andrew Gerrands Blog Post "JSON and Go".

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线