解析JSON时间错误

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

Parsing JSON Time error

问题

我正在尝试解析一个在Python中生成并发送到这里以存储在Cassandra中的时间戳,但是我遇到了一个奇怪的错误。

将时间戳“2015-11-05 14:14:32-0700”解析为“2006-01-02T15:04:05Z07:00”时出错:无法将“14:14:32-0700”解析为“T”。

我不想以任何方式修改从Python传递给我的时间,只想将其直接传递给Cassandra,但是驱动程序说我需要将其设置为time.Time(而不是字符串)。

以下是代码:

type OurCustomType struct {
    TimeStamp time.Time `json:"timestamp"`
}

func extractJsonBody(r *http.Request) []byte {
    body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
    if err != nil {
        //...
    }
    if err := r.Body.Close(); err != nil {
        //....
    }
    return body
}

func View(w http.ResponseWriter, r *http.Request) {
    var variable OurCustomType

    body := extractJsonBody(r)

    if err := json.Unmarshal(body, &variable); err != nil {
        //..
    } else {
        //...
    }
}

**编辑:**我已经实现了自己的time.Time类型,并实现了自定义的UnmarshalJSON来尝试解决这个问题。

type PythonTime struct {
    time.Time
}

func (self *PythonTime) UnmarshalJSON(b []byte) (err error) {
    self.Time, err = time.Parse("2006-01-02 15:04:05-0700", string(b))
    return
}

但是我得到了这个错误:

将时间“2015-11-05 14:14:32-0700”解析为“2006-01-02 15:04:05-0700”时出错:无法将“2015-11-05 14:14:32-0700”解析为“2006”。

英文:

I am trying to parse a timestamp that is generated in python and posted over to here to be stored in cassandra and I am getting a weird error.

parsing time ""2015-11-05 14:14:32-0700"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 14:14:32-0700"" as "T"

I don't want to modify the time given to me from the python in anyway, just want to pass it right into cassandra but the driver says I need to have it be time.Time (not string) to do so.

Here is the code

type OurCustomType struct {
	TimeStamp   time.Time `json:"timestamp"`
}


func extractJsonBody(r *http.Request) []byte {
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		//...
	}
	if err := r.Body.Close(); err != nil {
         //....
	}
	return body
}

func View(w http.ResponseWriter, r *http.Request) {
	var variable OurCustomType

	body := extractJsonBody(r)

	if err := json.Unmarshal(body, &variable); err != nil {
		//..
	} else {
	    //...
	}
}

Edit: I have implemented my own time.Time type and implemented a custom UnmarshalJSON to attempt to fix this issue.

type PythonTime struct {
	time.Time
}

func (self *PythonTime) UnmarshalJSON(b []byte) (err error) {
	self.Time, err = time.Parse("2006-01-02 15:04:05-0700", string(b))
	return
}

But I am not getting this error:

parsing time ""2015-11-05 14:14:32-0700"" as "2006-01-02 15:04:05-0700": cannot parse ""2015-11-05 14:14:32-0700"" as "2006"

答案1

得分: 0

问题在于你在从Python传递的日期中使用了引号。

在你发布的第一个错误中,你可以看到它在读取"T"时失败了。这是因为你在Go中使用的格式与你从Python返回的格式不同。

""2015-11-05 14:14:32-0700""解析为""2006-01-02T15:04:05Z07:00""时出错:无法将14:14:32-0700解析为T

你在Go中使用了""2006-01-02T15:04:05Z07:00"",而Python返回的格式是""2006-01-02 15:04:05-0700""

在你最后一个错误中也是如此,只是你在日期格式中省略了一对引号,与你从Python返回的内容不符。

""2015-11-05 14:14:32-0700""解析为2006-01-02 15:04:05-0700时出错:无法将""2015-11-05 14:14:32-0700""解析为2006

这意味着你提供了"2006-01-02 15:04:05-0700",但它期望的是""2006-01-02 15:04:05-0700"",因为Python返回的日期中有额外的引号。

英文:

The problem here is that you've got quotations in the date being passed from Python.

In the first error that you posted, you can see that it failed at reading "T". That's because you were using a different format in Go than you were returning from Python.

parsing time ""2015-11-05 14:14:32-0700"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 14:14:32-0700"" as "T"

You used ""2006-01-02T15:04:05Z07:00"" when the format from Python is ""2006-01-02 15:04:05-0700"".

The same is true in your last error, except that you dropped the one pair of the quotations in the date format verses what you have returned in Python.

parsing time ""2015-11-05 14:14:32-0700"" as "2006-01-02 15:04:05-0700": cannot parse ""2015-11-05 14:14:32-0700"" as "2006"

This is saying that you provided "2006-01-02 15:04:05-0700" but it's expecting ""2006-01-02 15:04:05-0700"" due to the extra quotes in the date returned by Python.

huangapple
  • 本文由 发表于 2015年11月19日 05:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/33791172.html
匿名

发表评论

匿名网友

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

确定