为了账号安全,请及时绑定邮箱和手机立即绑定

取消编组嵌套 xml 戈朗

取消编组嵌套 xml 戈朗

Go
互换的青春 2022-09-12 21:05:46
我想将嵌套的 xml 解封为戈朗结构。我想要取消编组的 xml 如下所示。<?xml version="1.0" encoding="UTF-8"?><status>    <authorized>true</authorized>    <plan>Basic</plan>    <usage_reports>        <usage_report metric="hits" period="day">            <period_start>2021-07-20 00:00:00 +0000</period_start>            <period_end>2021-07-21 00:00:00 +0000</period_end>            <max_value>1000000</max_value>            <current_value>0</current_value>        </usage_report>        <usage_report metric="hits.82343" period="day">            <period_start>2021-07-20 00:00:00 +0000</period_start>            <period_end>2021-07-21 00:00:00 +0000</period_end>            <max_value>1000000</max_value>            <current_value>0</current_value>        </usage_report>    </usage_reports></status>为了取消马歇尔,我定义了两个围棋结构,如下所示。// UsageReport represents the usage report of a particular metric.type UsageReport struct {    Usage   string `xml:"usage_report,chardata"`    Metric  string `xml:"metric,attr"`    Period  string `xml:"period,attr"`    Start   string `xml:"period_start"`    End     string `xml:"period_end"`    Max     int64  `xml:"max_value"`    Current int64  `xml:"current_value"`}// AuthResponse represents the structure of the response from authorize calls.type AuthResponse struct {    Status     xml.Name      `xml:"status"`    Authorized bool          `xml:"authorized"`    Plan       string        `xml:"plan"`    Usages     []UsageReport `xml:"usage_reports"`}但是当我尝试使用 解组时,只有 和 值被取消组。解组的结果如下所示。xml.UnmarshalPlanAuthorizedXML: {{ } true Basic []}
查看完整描述

2 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

试试这个游乐场。我使用此工具作弊并帮助我从XML数据生成Go类型。

package main


import (

    "encoding/xml"

    "fmt"

)


type Status struct {

    XMLName      xml.Name `xml:"status"`

    Text         string   `xml:",chardata"`

    Authorized   string   `xml:"authorized"`

    Plan         string   `xml:"plan"`

    UsageReports struct {

        Text        string `xml:",chardata"`

        UsageReport []struct {

            Text         string `xml:",chardata"`

            Metric       string `xml:"metric,attr"`

            Period       string `xml:"period,attr"`

            PeriodStart  string `xml:"period_start"`

            PeriodEnd    string `xml:"period_end"`

            MaxValue     string `xml:"max_value"`

            CurrentValue string `xml:"current_value"`

        } `xml:"usage_report"`

    } `xml:"usage_reports"`

}


func main() {

    status := Status{}

    err := xml.Unmarshal([]byte(xmlStr), &status)

    if err != nil {

        panic(err)

    }

    fmt.Println(status.UsageReports.UsageReport[0].MaxValue)

}


const xmlStr = `<?xml version="1.0" encoding="UTF-8"?><status><authorized>true</authorized><plan>Basic</plan><usage_reports><usage_report metric="hits" period="day"><period_start>2021-07-20 00:00:00 +0000</period_start><period_end>2021-07-21 00:00:00 +0000</period_end><max_value>1000000</max_value><current_value>0</current_value></usage_report><usage_report metric="hits.82343" period="day"><period_start>2021-07-20 00:00:00 +0000</period_start><period_end>2021-07-21 00:00:00 +0000</period_end><max_value>1000000</max_value><current_value>0</current_value></usage_report></usage_reports></status>`



查看完整回答
反对 回复 2022-09-12
?
手掌心

TA贡献1942条经验 获得超3个赞

为了获得字段的值,请根据姆科普里瓦的建议进行修改。Usagesxml:"usage_reports"xml:"usage_reports>usage_report"


package main


import (

    "encoding/xml"

    "fmt"

)


type UsageReport struct {

    Metric  string `xml:"metric,attr"`

    Period  string `xml:"period,attr"`

    Start   string `xml:"period_start"`

    End     string `xml:"period_end"`

    Max     int64  `xml:"max_value"`

    Current int64  `xml:"current_value"`

}


type AuthResponse struct {

    Status     xml.Name      `xml:"status"`

    Authorized bool          `xml:"authorized"`

    Plan       string        `xml:"plan"`

    Usages     []UsageReport `xml:"usage_reports>usage_report"`

}


var data = []byte(`<?xml version="1.0" encoding="UTF-8"?>

<status>

    <authorized>true</authorized>

    <plan>Basic</plan>

    <usage_reports>

        <usage_report metric="hits" period="day">

            <period_start>2021-07-20 00:00:00 +0000</period_start>

            <period_end>2021-07-21 00:00:00 +0000</period_end>

            <max_value>1000000</max_value>

            <current_value>0</current_value>

        </usage_report>

        <usage_report metric="hits.82343" period="day">

            <period_start>2021-07-20 00:00:00 +0000</period_start>

            <period_end>2021-07-21 00:00:00 +0000</period_end>

            <max_value>1000000</max_value>

            <current_value>0</current_value>

        </usage_report>

    </usage_reports>

</status>

`)


func main() {

    r := AuthResponse{}

    if err := xml.Unmarshal(data, &r); err != nil {

        panic(err)

    }

    fmt.Println(r)

}

输出:


{{ } true Basic [{hits day 2021-07-20 00:00:00 +0000 2021-07-21 00:00:00 +0000 1000000 0} {hits.82343 day 2021-07-20 00:00:00 +0000 2021-07-21 00:00:00 +0000 1000000 0}]}



查看完整回答
反对 回复 2022-09-12
  • 2 回答
  • 0 关注
  • 154 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号