json


Golang date time struct


I'm trying to build golang date time struct and I'm having some issues. Please help me build the model.
Thanks again
"effectiveDates" : {
"startDate" : {
"_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl",
"year" : NumberInt(2008),
"month" : NumberInt(10),
"day" : NumberInt(16),
"timezone" : NumberInt(-300),
"hour" : NumberInt(9),
"minute" : NumberInt(50),
"second" : NumberInt(13)
}
}
This should be enough to get you going. There are a couple quirks:
The NumberInt(n) values are not valid JSON, so I've assumed they come in from the GET call as a number
The timezone NumberInt will need to be converted to a string, and massaged into the form -0700. I've left that as an exercise for you
Here's my solution:
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))
}

Related Links

How to Parse RSS Data with JSON
Facebook Graph API inbox messages with no comments array
extjs nested model
wikipedia API: parse a table as JSON?
give json data to jqGrid view once
How do I update one controller with data from another controller with a service in angular?
ExtJS Specify the existing date format for conversion
Binding viewmodel with knockoutjs mapping
Bug in twitteR and sentiment analysis code [closed]
What's wrong with this JSON object?
Using ModelCompanion's fromJSON method in Play application throws a NoSuchMethodException
How do I parse this JSON-string in Go?
Sencha store and model set up for use with Json
Grails rendering JSON from domain list weird behaviour
properties file to json [closed]
Action composition in Play framework with Java

Categories

HOME
cntk
app-inventor
erlang
elixir
npm
nlp
backup
deep-linking
computer-vision
parse-server
ruby-on-rails-3
appmaker
specflow
java-home
onenote-api
deeplearning4j
android-viewpager
pjsip
game-maker-studio-1.4
public-key-encryption
x-frame-options
cosmicmind
typeahead
riak
uicollectionview
session-timeout
arraylist
ng2-dragula
nmake
jquery-form-validator
data-cleansing
guile
ui5
unpack
chrome-native-messaging
positioning
percentage
fabric-digits
liquid-xml
linq-to-entities
wsadmin
tango
catia
context-free-language
automator
tableau-server
defold
nstouchbar
tiddlywiki
decompress
tcserver
crash-reports
heroku-postgres
mercury
pdflib
cfeclipse
hspec
freetype2
database-backups
emokit
codesign
asp.net-mvc-partialview
galaxy
throughput
data-import
matcaffe
registrykey
programming-paradigms
achartengine
team-build
ytplayerview
rspec3
custom-url
robocode
kallithea
mser
bsp
korma
reserved-words
json-patch
hamsterdb
convex-polygon
loop-invariant
jqmobi
ftp4j
vmware-server
fluidsynth
sametime
ubuntu-11.10
usn
xgettext
cosm
macruby
concurrent-programming
webkit-transform
goliath
querypath
external-assemblies
dirty-data
divx
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App