关于curl对go语言转换,再对request进行结构体转化为json序列化,初始为var data = strings.NewReader({"trans_type":"en2zh","source":"good"})
再将其转化为结构体,type DictRequest struct { Transtype string json:"trans_type"
Source string json:"source"`` },然后再进行request更改并序列化 request := DictRequest{Transtype: "en2zh", Source: word}
- `buf, err := json.Marshal(request)
- `if err != nil {
-
log.Fatal(err) - `}
var data = bytes.NewReader(buf)- 接下来,把response反序列化,对网页返回的preview里面将如下
{"rc": 0,"wiki": {},"dictionary":{``"prons": {"en-us": "[gʊd]","en": "[gud]"},"antonym": ["bad","wrong","evil","harmful","poor"],``"entry": "good","explanations": [`` "a.好的;善良的;快乐的;真正的;宽大的;有益的;老练的;幸福的;忠实的;优秀的;完整的;彻底的;丰富的","n.利益;好处;善良;好人","ad.=well"],"prons": {"en-us": "[gʊd]","en": "[gud]"},"related": [],"source": "wenquxing",``"synonym": [``"excellent","fine","nice","splendid","proper"],"type": "word","wqx_example": [` ["to the good", "有利,有好处"],` ` ["good, bad and indifferent", "好的,坏的和一般的"], ` ["good innings", "长寿"]- ` ] } }- 然后转化成结构体
type DictResponse struct {Rc intjson:"rc"Wiki struct { }json:"wiki"Dictionary struct { Prons struct { EnUs stringjson:"en-us"En stringjson:"en"}json:"prons"Antonym []stringjson:"antonym"Entry stringjson:"entry"Explanations []stringjson:"explanations"Related []interface{}json:"related"Source stringjson:"source"Synonym []stringjson:"synonym"Type stringjson:"type"WqxExample [][]stringjson:"wqx_example"}json:"dictionary<p align=left>"``</p> } - 再将其反序列化
- `var dicresponse DictResponse
- err = json.Unmarshal(bodyText, &dicresponse)
- if err != nil {
-
log.Fatal(err) - }`
- 最后根据需求打印
- `fmt.Println("UK:", dicresponse.Dictionary.Prons.En, "US:", dicresponse.Dictionary.Prons.EnUs)
- for _, item := range dicresponse.Dictionary.Explanations {
-
fmt.Println(item) - }