第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法從 GO 中的 XML 中提取正確的數(shù)據(jù)

無法從 GO 中的 XML 中提取正確的數(shù)據(jù)

Go
達令說 2021-06-03 17:38:55
我正在從 Web 檢索一些 XML,但在提取所需數(shù)據(jù)時遇到問題。這是 XML:<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0" created="2013-04-13T16:54:01.107Z">    <artist-list count="2" offset="0">        <artist id="35dac7d2-0b1f-470f-9a5a-c53c8821f6d6" type="Person" ext:score="100">            <name>Eric Prydz</name>            <sort-name>Prydz, Eric</sort-name>            <gender>male</gender>            <country>SE</country>        </artist>    </artist-list></metadata>我想提取姓名、性別和國家。這是代碼:package mainimport (    "encoding/xml"    "fmt"    "io/ioutil"    "net/http")func main() {    client := &http.Client{}    req, _ := http.NewRequest("GET", "http://www.musicbrainz.org/ws/2/artist/?query=artist:Fred", nil)    res, _ := client.Do(req)    bs, _ := ioutil.ReadAll(res.Body)    var artist Artist    xml.Unmarshal(bs, &artist)    fmt.Printf("%#v\n", artist)}type Artist struct {    Name    string `xml: "name"`    Gender  string `xml: "gender"`    Country string `xml: "country"`}但每次我運行這個我總是得到這個:main.Artist{Name:"", Gender:"", Country:""}有人可以指出問題出在哪里嗎?
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經(jīng)驗 獲得超4個贊

好的,問題是,您沒有充分描述xml要解組的數(shù)據(jù)。你的數(shù)據(jù)看起來更像


struct metadata {

    // you need to tag it because go field names can't contain -'s

    artists []Artist "artist-list"

}

這樣的事情應(yīng)該有效?;旧希琔nmarshal只會看頂層節(jié)點,而不是走下來尋找結(jié)構(gòu)。


查看完整回答
反對 回復(fù) 2021-06-07
  • 1 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號