xml处理
import (
"encoding/json"
"encoding/xml"
"fmt"
)
//<string xmlns="http://www.baidu.com">{"Flag":"Success","Message":""}</string>
func GetInnerText(str string) (restr string) {
var rb []byte = []byte(str)
var result StringResources
xml.Unmarshal(rb, &result)
//获取节点d 单个值
fmt.Println(result.XMLName)//http://www.baidu.com
fmt.Println(result.InnerText)//{"Flag":"Success","Message":""}
restr = result.InnerText
return
}
type StringResources struct {
XMLName xml.Name `xml:"string"`
StringName string `xml:"xmlns,attr"`
InnerText string `xml:",innerxml"`
// ResourceString []ResourceString `xml:"string"`
}
json处理
//构造体转换成json字符串
func Convert2Jsonstr(v interface{}) (string, error) {
jsons, errs := json.Marshal(v)
if errs != nil {
fmt.Println(errno.ModelError)
return "", errno.ModelError
}
return string(jsons), nil
}
//json字符串转换成map[string]string
func ConvertString2Map(str string) (map[string]string, error) {
var info map[string]string
errs := json.Unmarshal([]byte(str), &info)
if errs != nil {
fmt.Println(errs)
return nil, errs
}
return info, nil
}
type Material struct {
MCard string `json:"MCard"` //物料编码
}
//json字符串转换成[]Material
func ConvertString2MaterialArray(str string) ([]Material, error) {
var info []Material
errs := json.Unmarshal([]byte(str), &info)
if errs != nil {
fmt.Println(errno.ModelError)
return nil, errno.ModelError
}
return info, nil
}