猿问

golang - 解析复杂json

代码在这里http://play.golang.org/p/l1__b2FOsv
//Clickhereandstarttyping.
packagemain
import"fmt"
import"encoding/json"
typeMxRecordsstruct{
valuestring
ttlint
priorityint
hostNamestring
}
typeDatastruct{
mxRecords[]MxRecords
}
typeResponsestruct{
Statusstring`json:"status"`
DataData`json:"data"`
}
typeapiRstruct{
ResponseResponse`json:"response"`
}
funcmain(){
body:=`
{"response":{
"status":"SUCCESS",
"data":{
"mxRecords":[
{
"value":"us2.mx3.mailhostbox.com.",
"ttl":1,
"priority":100,
"hostName":"@"
},
{
"value":"us2.mx1.mailhostbox.com.",
"ttl":1,
"priority":100,
"hostName":"@"
},
{
"value":"us2.mx2.mailhostbox.com.",
"ttl":1,
"priority":100,
"hostName":"@"
}
],
"cnameRecords":[
{
"aliasHost":"pop.a.co.uk.",
"canonicalHost":"us2.pop.mailhostbox.com."
},
{
"aliasHost":"webmail.a.co.uk.",
"canonicalHost":"us2.webmail.mailhostbox.com."
},
{
"aliasHost":"smtp.a.co.uk.",
"canonicalHost":"us2.smtp.mailhostbox.com."
},
{
"aliasHost":"imap.a.co.uk.",
"canonicalHost":"us2.imap.mailhostbox.com."
}
],
"dkimTxtRecord":{
"domainname":"20a19._domainkey.a.co.uk",
"value":"\"v=DKIM1;g=*;k=rsa;p=DkfbhO8Oyy0E1WyUWwIDAQAB\"",
"ttl":1
},
"spfTxtRecord":{
"domainname":"a.co.uk",
"value":"\"v=spf1redirect=_spf.mailhostbox.com\"",
"ttl":1
},
"loginUrl":"us2.cp.mailhostbox.com"
}
}}`
varrapiR
err:=json.Unmarshal([]byte(body),&r)
iferr!=nil{
fmt.Printf("errwas%v",err)
}
fmt.Println(r)
}
解析出来data为空,这是为何?
不用interface该如何解析
慕哥9229398
浏览 1065回答 2
2回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答