這應該足以讓你去。有一對夫婦怪癖:
- 的NumberInt(n)的值不是有效的JSON,所以我認爲他們從一開始呼叫進來多項
- 的時區NumberInt將需要轉換爲一串,並按-0700的形式。我已經離開,作爲一個練習吧
,這裏是我的解決方案:
package main
import (
"encoding/json"
"fmt"
"time"
)
type StartDate struct {
Class string `json:"_class"`
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Timezone string `json:"timezone"`
Hour int `json:"hour"`
Minute int `json:"minute"`
Second int `json:"second"`
}
type EffectiveDates struct {
StartDate `json:"startDate"`
}
type Payload struct {
EffectiveDates `json:"effectiveDates"`
}
var input = `{
"effectiveDates" : {
"startDate" : {
"_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl",
"year" : 2008,
"month" : 10,
"day" : 16,
"timezone" : "-0300",
"hour" : 9,
"minute" : 50,
"second" : 13
}
}
}`
func main() {
var p Payload
var t time.Time
err := json.Unmarshal([]byte(input[:]), &p)
if err != nil {
fmt.Println("error:", err)
}
timeStr := fmt.Sprintf("%d-%d-%d %d:%d:%d %s", p.Year, p.Month, p.Day, p.Hour, p.Minute, p.Second, p.Timezone)
if t, err = time.Parse("2006-01-02 3:04:05 -0700", timeStr); err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%s", t.Format(time.RFC3339))
}
有沒有你不使用標準庫'Time'反對理由嗎?它已經擁有了所有這些。 https://golang.org/pkg/time/ – evanmcdonnal
對象正在被另一個進程構建,並被轉儲到mongoDB中。我試圖建立一個GET golang服務來檢索。目前,我無法控制POST。 – user3066155
我無法使用http://json2struct.mervine.net/或https://mholt.github.io/json-to-go/進行轉換,我無法弄清楚如何構建。 – user3066155