dongwei2610 2013-08-15 08:27
浏览 127
已采纳

如何在golang中将数据结构作为参数传递

XML to Json

package main

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
)

type Persons struct {
    Person []struct {
        Name string
        Age  int
    }
}
type Places struct {
    Place []struct {
        Name    string
        Country string
    }
}

type Parks struct {
    Park struct {
        Name     []string
        Capacity []int
    }
}

const personXml = `
    <Persons>
        <Person><Name>Koti</Name><Age>30</Age></Person>
        <Person><Name>Kanna</Name><Age>29</Age></Person>
    </Persons>
`

const placeXml = `
    <Places>
        <Place><Name>Chennai</Name><Country>India</Country></Place>
        <Place><Name>London</Name><Country>UK</Country></Place>
    </Places>
`

const parkXml = `
    <Parks>
        <Park><Name>National Park</Name><Capacity>10000</Capacity></Park>
        <Park>Asian Park</Name><Capacity>20000</Capacity></Park>
    </Parks>
`

func WhatIamUsing() {
    var persons Persons
    xml.Unmarshal([]byte(personXml), &persons)
    per, _ := json.Marshal(persons)
    fmt.Printf("%s
", per)

    var places Places
    xml.Unmarshal([]byte(placeXml), &places)
    pla, _ := json.Marshal(places)
    fmt.Printf("%s
", pla)

    var parks Parks
    xml.Unmarshal([]byte(parkXml), &parks)
    par, _ := json.Marshal(parks)
    fmt.Printf("%s
", par)
}

What i want is a generic function which takes xml string and dataStruct and returns a Json output. But below function is throwing an error How to impliment this?

func Xml2Json(xmlString string, DataStruct interface{}) (jsobj string, err error) {
    var dataStruct DataStruct
    xml.Unmarshal([]byte(personXml), &dataStruct)
    js, _ := json.Marshal(dataStruct)
    return fmt.Sprintf("%s
", js), nil
}

func main() {
    jsonstring, _ := Xml2Json(personXml, Persons)
}

Error message:

prog.go:73: DataStruct is not a type

prog.go:80: type Persons is not an expression

goplay link: http://play.golang.org/p/vayb0bawKx

  • 写回答

2条回答 默认 最新

  • duangu9997 2013-08-15 08:48
    关注

    You can not store a type (like Persons) in an interface. You could pass a reflect.Type to your function. Then, your call would look like Xml2Json(personXml, reflect.TypeOf(Persons)) which is quite ugly in my opinion.

    The better approach is probably:

    func Xml2Json(xmlString string, value interface{}) (string, error) {
        if err := xml.Unmarshal([]byte(xmlString), value); err != nil {
            return "", err
        }
        js, err := json.Marshal(value)
        if err != nil {
            return "", err
        }
        return string(js), nil
    }
    

    You can use this function with Xml2Json(personXml, new(Persons)) if you are not interested in the value itself, and

    var persons Persons
    Xml2Json(personXML, &persons)
    

    when you also want to retrieve the struct value for later processing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多