2016-05-04 19:58
浏览 108


I'm attempting to write code to translate XML to JSON. The XML I'm trying to translate is as follows...

(Just a snippet)

        <title>Weather Underground</title>
        <full>Kearney, MO</full>

Current Code:

`package main

import (

type reportType struct{
    Version xml.CharData        `xml:"version"`
    TermsOfService xml.CharData `xml:"termsofService"
    Features xml.CharData       `xml:"features>feature"`
    Full     xml.CharData       `xml:"current_observation>display_location>full"`
    StateName xml.CharData      `xml:"current_observation>display_location>state_name"`
    WindGust xml.CharData       `xml:"current_observation>observation_location>full"`
    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    map[string]string `json:"features"`;
    Full        map[string]string `json:"display_location"`;
    WindGust map[string]string `json:"observation_location"`

func main() {
    fmt.Println("data is from WeatherUnderground.")
    var state, city string
    str1 := "What is your state?"
    str2 := "What is your city?"
    fmt.Scanf("%s", &state)
    fmt.Scanf("%s", &city)
    baseURL := "";
    apiKey := "3hunna"
    var query string

    //set up the query
    query = baseURL+apiKey +
    url.QueryEscape(state)+ "/"+
    url.QueryEscape(city)+ ".xml"
    fmt.Println("The escaped query: "+query)

    response, err := http.Get(query)
    doErr(err, "After the GET")
    var body []byte
    body, err = ioutil.ReadAll(response.Body)
    doErr(err, "After Readall")
    fmt.Printf("The body: %s

    var report reportType
    xml.Unmarshal(body, &report)
    fmt.Printf("The Report: %s
", report)
    fmt.Printf("The description is [%s]

    //Now marshal the data out in JSON
    var data []byte
    var output reportTypeJson
    output.Version = string(report.Version);
    output.TermsOfService = string(report.TermsOfService)

    output.Features= map[string]string{"feature":string(report.Features)} // allocate a map, add the 'features' value to it and assign it to output.Features
    data,err = json.MarshalIndent(output,"","      ")
    doErr(err, "From marshalIndent")
    fmt.Printf("JSON output nicely formatted: 

func doErr( err error, message string){
    if err != nil{
        log.Panicf("ERROR: %s %s 
", message, err.Error())


As you can see, I'm using maps to map one level nesting such as in the features case. But for in two level nesting cases such as xml:"current_observation>display_location>state_name", I can't figure out how to create the very first level, in this case current_observations. Would there be a way to somehow create a map of maps of sorts? Any and all ideas are much appreciated because I am very confused at the moment, Thanks for your time!

And the Output:

JSON output nicely formatted: 
      "version": "0.1",
      "termsofService": "",
      "features": {
            "feature": "conditions"
      "display_location": {
            "full": "Kearney, MO",
            "state_name": "Missouri"
      "observation_location": {
            "full": "Stonecrest, Kearney, Missouri"
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanliao3826 2016-05-04 20:12

    You could use either structs or a map of maps. I'll give some examples of both, starting with the map of maps. The type would be declares as;

    CurrentObservation map[string]map[string]string `json:"current_observation"`

    In this case you have a map with strings as the keys and the value is another map that has string for both key and value. As a result when you marshal your json you will end up with something like;

    "current_observation" {
         "image": { // first level where the value is a map[string]string
              "title":"Weather Underground" // inner map, string to string

    If say you wanted to just print the title, you would do;


    Since the data there looks fairly static you could also use structs instead. In which case you'd use something like this;

    CurrentObservation CurrentObservation `json:"current_observation"`
    type CurrentObservation struct {
        Image Image `json:"image"`
    type Image struct {
        Url string `json:"url"`
        Title string `json:"title"`
        Link string `json:"link"`

    Both options produce the same output though they could behave differently for different inputs. For example, if another version of current_observation were received as input that for example has another nested item in it call it... previous_observation then the map option would automatically unmarhsal this data as well where the struct options would exclude it since there would be no mapping to any object/type in Go.

    Personally I prefer the struct route when possible but it varies from case to case. For your application the map is likely better since you're not working with the input (it comes in as xml) and you just want to print it, you don't really have to deal with the details of current_observation, if it has 3 objects inside it, they'll all be output as expected, if it were 5 it would be the same. With the structs you have to explicitly define every single field which isn't really necessary if you're just transforming the input. The advantage of the struct is more for use later on where you have type safety although in this case, I would say they're still fairly equivalent because for example anytime you want to access something in image, like CurrentObservation.Image.Title you'll have to perform a check to ensure Image is not nil, like;

    if CurrentObservation.Image != nil {

    With the map you basically have the same overhead, only you're checking for the existence of a key rather than checking if one of the inner structs is nil or not.

    EDIT: example of initializing the map of maps using composite literal syntax;

       reportJson.CurrentObservation := map[string]map[string]string {
               "display_location": map[string]string {
                    "full": report.Full,
                    "state_name": report.StateName,
    点赞 打赏 评论

相关推荐 更多相似问题