反序列化的JSON返回为空。

huangapple go评论97阅读模式
英文:

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
		}
	}
}

huangapple
  • 本文由 发表于 2012年12月7日 18:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/13760884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定