猿问

GOlang:将 XML 嵌套到 JSON


我试图弄清楚如何给出 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"

}


谢谢你的时间!


FFIVE
浏览 291回答 1
1回答

凤凰求蛊

您没有得到所需的输出,因为您的 json 结构的定义不正确。你有;type reportTypeJson struct{&nbsp; &nbsp; Version&nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; `json:"version"`;&nbsp; &nbsp; TermsOfService string `json:"termsofService"`;&nbsp; &nbsp; Features&nbsp; &nbsp; string `json:"features feature" `;&nbsp; &nbsp; Zip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"current_observation > display_location > zip"`;}它将特征表示为字符串,但它实际上是一个对象,要么是一个对象,要么是map[string]string它自己的结构体,可以这样定义;type Features struct {&nbsp; &nbsp; &nbsp;Feature string `json:"feature"`}鉴于字段名称是复数,我猜它是一个集合,因此将您的结构更改为type reportTypeJson struct{&nbsp; &nbsp; Version&nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; `json:"version"`;&nbsp; &nbsp; TermsOfService string `json:"termsofService"`;&nbsp; &nbsp; Features&nbsp; &nbsp; map[string]string `json:"features"`;&nbsp; &nbsp; Zip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"current_observation > display_location > zip"`;}可能是您正在寻找的。当然,这意味着您必须修改一些其他代码,这些代码将 xml 结构中的值分配给 json 或其他代码,但我认为您可以自己解决这个问题:D编辑:下面的部分是您将 xml 类型转换为 json 类型的地方(即分配 reportTypeJson 的实例并将 reportType 的值分配给它,以便您可以调用其上的 json marshal 以产生输出)。假设你正在使用的定义reportTypeJson从上面它有Features一个map[string]string,你只需要修改你设置一条线output.Features。在下面的示例中,我使用“复合文字”语法内联地执行此操作。这允许您实例化/分配集合,同时为其分配值。//Now marshal the data out in JSONvar data []bytevar output reportTypeJsonoutput.Version = string(report.Version);output.TermsOfService = string(report.TermsOfService)output.Features= map[string]string{"features":string(report.Features)} // allocate a map, add the 'features' value to it and assign it to output.Featuresoutput.Zip=string(report.Zip)data,err = json.MarshalIndent(output,"","&nbsp; &nbsp; &nbsp; ")doErr(err, "From marshalIndent")fmt.Printf("JSON output nicely formatted: \n%s\n",data)
随时随地看视频慕课网APP

相关分类

Go
我要回答