我想從 Go 中的 XML 文檔創(chuàng)建 JSON 對象。現(xiàn)在我正在做的是使用 xml.Unmarshall 函數(shù)獲取結構對象中的 XML 數(shù)據(jù),然后使用 fmt.Sprintf 函數(shù)以編程方式格式化 JSON 結構中的字符串。這段代碼不可讀,我覺得應該有更好的方法來做到這一點。有人可以建議更好的東西。我目前的代碼是var root Root_ = xml.Unmarshal(data, &root)fmt.Fprintln(w, fmt.Sprintf("{\"type\": \"%s\", \"action\": \"save\", \"entry\": {\"ads_enabled\": 1, \"comments_enabled\": 0, \"cover_headline\": \"%s\", }}", root.Type, root.SeoHeadline, //coverheadline ))type Root struct { Type string `xml:"type,attr" json:"type"` CoverHeadline string `xml:"Head>PageHeadline>p" json:"cover_headline"`}其中 data 是 byte[] 對象
- 1 回答
- 0 關注
- 357 瀏覽
添加回答
舉報
0/150
提交
取消