在golang的dropbox库中获取日期时出现的Json问题。

huangapple go评论74阅读模式
英文:

Json issue in getting date in golang dropbox library

问题

我正在使用Dropbox API编写一个小程序来学习Go语言。我正在使用这里的客户端库:https://github.com/stacktic/dropbox。

我能够上传和下载文件,所以我知道我的API密钥等都是正确的。使用Metadata方法,我可以获取文件的元数据。然而,当我尝试使用UnmarshalJSON方法从entry结构体的ClientMtime项中获取可读的日期时,我得到了"unexpected end of JSON input"的错误。有什么问题吗?

我使用的代码如下:

func main() {
  db := dropbox.NewDropbox()
  db.SetAppInfo("Blah", "blah")
  db.SetAccessToken("Token")
  list, err := db.Metadata("/app_folder/test.jpg", true, false, "", "", 1)

  if err != nil {
    log.Fatal(err)
  }

  var date []byte
  err = list.ClientMtime.UnmarshalJSON(date)

  if err != nil {
    log.Fatal(err)
  }

  fmt.Printf("%v", date)
}

谢谢!

英文:

I'm writing a little program using the dropbox api to learn go. I'm using the client library here: https://github.com/stacktic/dropbox.

I'm able to upload and download a file so I know my api keys and what not are working correctly. Using the Metadata method I can get the metadata for a file. However, when I try to use the UnmarshalJSON method to get a human readable date from the ClientMtime item in the entry struct, I get "unexpected end of JSON input". Any ideas on what's the issue?

The code I'm using is as follows:

func main() {

  db := dropbox.NewDropbox()
  db.SetAppInfo("Blah", "blah")
  db.SetAccessToken("Token")
  list,err := db.Metadata("/app_folder/test.jpg", true, false, "", "", 1)

  if err != nil {
    log.Fatal(err)
  }

  var date []byte
  err = list.ClientMtime.UnmarshalJSON(date)

  if err != nil {
    log.Fatal(err)
  }

  fmt.Printf("%v", date)
}

Thanks!

答案1

得分: 1

你想要的是:

date, err := list.ClientMtime.MarshalJSON()

UnmarshalJson 则是相反的过程;[]byte -> DBTime
这就是为什么会出现输入结束错误,因为 []byte 是空的。

另外,ClientMTime 是一个时间。时间具有 String()Format() 方法。

你可以通过转换来访问所有的时间格式化功能。

参考:https://github.com/stacktic/dropbox/blob/master/dropbox.go#L158

英文:

You want:

date, err := list.ClientMtime.MarshalJSON()

UnmarshalJson goes the other way; []byte -> DBTime
That's why it's an end of input error, the []byte is empty.

Optionally, ClientMTime is a time. Time which has String() and Format() methods.

You can access all the time formatting features by converting it.

See: https://github.com/stacktic/dropbox/blob/master/dropbox.go#L158

huangapple
  • 本文由 发表于 2016年2月14日 14:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/35389062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定