एक्सएमएल Jsonकैसे डेटा struct पैरामीटर के रूप में golang में
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\n", per)
var places Places
xml.Unmarshal([]byte(placeXml), &places)
pla, _ := json.Marshal(places)
fmt.Printf("%s\n", pla)
var parks Parks
xml.Unmarshal([]byte(parkXml), &parks)
par, _ := json.Marshal(parks)
fmt.Printf("%s\n", par)
}
को पारित करने के लिए क्या मैं चाहता हूँ एक सामान्य समारोह जो एक्सएमएल स्ट्रिंग और dataStruct लेता है और एक JSON उत्पादन देता है। लेकिन नीचे फंक्शन एक त्रुटि फेंक रहा है इसे कैसे लागू करें?
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\n", js), nil
}
func main() {
jsonstring, _ := Xml2Json(personXml, Persons)
}
त्रुटि संदेश:
prog.go: 73: 80: http://play.golang.org/p/vayb0bawKx
: प्रकार व्यक्तियों एक अभिव्यक्तिgoplay लिंक नहीं है DataStruct एक प्रकार
prog.go नहीं है