我试图弄清楚如何给出 XML 输入,使用 GOlang 将其转换为 JSON。例如...
<version>0.1</version>
<termsofService>http://www.wunderground.com/weather/api/d/terms.html</termsofService>
<features>
<feature>conditions</feature>
</features>
会变成
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": { "feature": "conditions" },
我得到了versionandtermsofservice正确,但我不知道如何返回嵌套的features. 这是我必须硬编码的东西吗?
代码:
type reportType struct{
Version xml.CharData `xml:"version"`
TermsOfService xml.CharData `xml:"termsofService"
`
Features xml.CharData `xml:"features>feature"`
Zip xml.CharData `xml:"current_observation>display_location>zip"`
Problem myErrorType `xml:"error"`
}
type myErrorType struct{
TypeOfError xml.CharData `xml:"type"`
Desciption xml.CharData `xml:"description"`
}
type reportTypeJson struct{
Version string `json:"version"`;
TermsOfService string `json:"termsofService"`;
Features string `json:"features feature" `;
Zip string `json:"current_observation > display_location > zip"`;
}
func main() {
fmt.Println("data is from WeatherUnderground.")
fmt.Println("https://www.wunderground.com/")
var state, city string
str1 := "What is your state?"
str2 := "What is your city?"
fmt.Println(str1)
fmt.Scanf("%s", &state)
fmt.Println(str2)
fmt.Scanf("%s", &city)
baseURL := "http://api.wunderground.com/api/";
apiKey := "YouDontNeedToKnow"
var query string
//set up the query
query = baseURL+apiKey +
"/conditions/q/"+
url.QueryEscape(state)+ "/"+
url.QueryEscape(city)+ ".xml"
}
输出:
JSON output nicely formatted:
{
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features \u003e feature": "conditions",
"current_observation \u003e display_location \u003e zip": "64068"
}
谢谢你的时间!