英文:
Unmarshalled JSON returns nothing
问题
我有以下结构:
type Translation struct{
Data string
Translations []struct{
TranslatedText string
SourceLanguage string
}
}
type InputText struct {
PlainText string
TargetLanguage string
Values url.Values
}
还有一个调用Google Translate API并返回JSON字符串的方法,我想要解析它:
func (i *InputText) TranslateString() (*Translation, error){
if len(i.PlainText) == 0 {
log.Fatal("未指定文本")
}
if len(i.TargetLanguage) == 0 {
log.Fatal("未指定目标语言")
}
i.Values = make(url.Values)
var v = i.Values
v.Set("target", i.TargetLanguage)
v.Set("key", API_KEY)
v.Set("q", i.PlainText)
u := fmt.Sprintf("%s?%s", api, v.Encode())
getResp, err := http.Get(u)
if err != nil{
log.Fatal("错误", err)
return nil, err
}
defer getResp.Body.Close()
body, _ := ioutil.ReadAll(getResp.Body)
t := new(Translation)
json.Unmarshal(body, &t)
return t, nil
}
func main(){
input := &InputText{"我的名字是约翰,我出生在内罗毕,我今年31岁", "ES", nil}
translation, _ := input.TranslateString()
fmt.Println(translation)
}
当我运行这段代码时,我只得到&{[]}
的输出,我以为JSON没有返回,但是当我在TranslateString()方法中尝试这样做时:
fmt.Println(string(body))
它输出:
{
"data": {
"translations": [
{
"translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
"detectedSourceLanguage": "en"
}
]
}
}
英文:
I have the following struct:
type Translation struct{
Data string
Translations []struct{
TranslatedText string
SourceLanguage string
}
}
type InputText struct {
PlainText string
TargetLanguage string
Values url.Values
}
and a method that hits the Google Translate API and returns JSON string that I want to UnMarshal:
func (i *InputText) TranslateString() (*Translation, error){
if len(i.PlainText) == 0 {
log.Fatal("No text specified")
}
if len(i.TargetLanguage) == 0 {
log.Fatal("No target language specified")
}
i.Values = make(url.Values)
var v = i.Values
v.Set("target", i.TargetLanguage)
v.Set("key", API_KEY)
v.Set("q", i.PlainText)
u := fmt.Sprintf("%s?%s", api, v.Encode())
getResp, err := http.Get(u)
if err != nil{
log.Fatal("error", err)
return nil, err
}
defer getResp.Body.Close()
body, _ := ioutil.ReadAll(getResp.Body)
t := new(Translation)
json.Unmarshal(body, &t)
return t, nil
}
func main(){
input := &InputText{"My name is John, I was born in Nairobi and I am 31 years old", "ES", nil}
translation, _ := input.TranslateString()
fmt.Println(translation)
}
When I run this code all I get is &{[]}
printed out, I thought the JSON wasn't being returned but when I tried this in the TranslateString() method:
fmt.Println(string(body))
It prints out:
{
"data": {
"translations": [
{
"translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
"detectedSourceLanguage": "en"
}
]
}
}
答案1
得分: 1
上面的代码是正确的,只有一个错误,我的结构体全部都错了!应该是:
type Translation struct{
Data struct {
Translations []struct {
TranslatedText string
DetectedSourceLanguage string
}
}
}
英文:
The code above is correct with one mistake, my struct is all wrong!! it should be:
type Translation struct{
Data struct {
Translations []struct {
TranslatedText string
DetectedSourceLanguage string
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论