将JSON响应表示为结构体

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

Represent JSON response as struct

问题

type Translations struct{
TranslatedText string json:"translatedText"
SourceLanguage string json:"detectedSourceLanguage"
}

type Translation struct{
Data struct{
Translations []Translations json:"translations"
} json:"data"
}

英文:

I'm doing a call to Google Translate API and would like to represent the response as a struct. The JSON response is:

{
 "data": {
  "translations": [
   {
    "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
    "detectedSourceLanguage": "en"
   }
  ]
 }
}

I've tried to come up with a struct:

type Translations struct{
  TranslatedText string
  SourceLanguage string
}

type Translation struct{
  Data string
  Value *[]Translations
}

or:

type Translations struct{
  TranslatedText string
  SourceLanguage string
}

type Translation struct{
  Data string
  Value Translations
}

Which is the correct way to do it?

答案1

得分: 4

这是一个工作示例,将两个结构定义合并为一个。

http://play.golang.org/p/nI0Qet6R78

package main

import (
	"fmt"
	"encoding/json"
	)

type Translation struct{
	Data struct {
		Translations []struct {
			TranslatedText string
			DetectedSourceLanguage string
		}
	}
}

func main() {

	source := []byte(`
	{
	 "data": {
	  "translations": [
	   {
	    "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
	    "detectedSourceLanguage": "en"
	   }
	  ]
	 }
	}
	`)

	var j Translation
	err := json.Unmarshal(source, &j)
	if err != nil {
		panic(err)
	}
	for _,t := range j.Data.Translations {
		fmt.Printf("----\n")
		fmt.Printf("translatedText: %s\n", t.TranslatedText)
		fmt.Printf("detectedSourceLanguage: %s\n", t.DetectedSourceLanguage)
	}
}
英文:

Here's a working example that consolidates the two struct definitions into one.

http://play.golang.org/p/nI0Qet6R78

package main

import (
	"fmt"
	"encoding/json"
	)

type Translation struct{
	Data struct {
		Translations []struct {
			TranslatedText string
			DetectedSourceLanguage string
		}
	}
}

func main() {

	source := []byte(`
	{
	 "data": {
	  "translations": [
	   {
	    "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
	    "detectedSourceLanguage": "en"
	   }
	  ]
	 }
	}
	`)

	var j Translation
	err := json.Unmarshal(source, &j)
	if err != nil {
		panic(err)
	}
	for _,t := range j.Data.Translations {
		fmt.Printf("----\n")
		fmt.Printf("translatedText: %s\n", t.TranslatedText)
		fmt.Printf("detectedSourceLanguage: %s\n", t.DetectedSourceLanguage)
	}
}

答案2

得分: 0

你可以将你的数据分成三个部分。一个单独的translation包含在一个translations数组中。你的主要结构是data

我发现从最内部的结构开始定义,然后逐步向外部结构工作是最容易的。

这些的表示可以像这样定义:

type translation struct {
	TranslatedText string
	SourceLanguage string `json:"detectedSourceLanguage"`
}

type translations struct {
	Translations []translation
}

type data struct {
	Data translations
}

如果数据结构不太复杂,你可以像这样将它们全部放在一起:

type data struct {
	Data struct {
		Translations []struct {
			TranslatedText string
			SourceLanguage string `json:"detectedSourceLanguage"`
		}
	}
}

在一个完整的示例中:

package main

import (
	"encoding/json"
	"fmt"
)

var json_s string = `{
  "data": {
    "translations": [
      {
        "translatedText": "Mi nombre es John, nací en Nairobi y tengo 31 años de edad",
        "detectedSourceLanguage": "en"
      }
    ]
  }
}`

type data struct {
	Data struct {
		Translations []struct {
			TranslatedText string
			SourceLanguage string `json:"detectedSourceLanguage"`
		}
	}
}

func main() {
	var translation data
	err := json.Unmarshal([]byte(json_s), &translation)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", translation)
}
英文:

You can break your data into three parts. A single translation which is contained in a translations array. Your major structure is data.

I find it most easy to start at the most inner structure and define that and then work my way to the outer structs.

The representation of those can be defined like this:

type translation struct {
	TranslatedText string
	SourceLanguage string `json:"detectedSourceLanguage"`
}

type translations struct {
	Translations []translation
}

type data struct {
	Data translations
}

If the data structure is not too complex you can put it all together like this:

type data struct {
	Data struct {
		Translations []struct {
			TranslatedText string
			SourceLanguage string `json:"detectedSourceLanguage"`
		}
	}
}

In a full working example:

package main

import (
	"encoding/json"
	"fmt"
)

var json_s string = `{
  "data": {
    "translations": [
      {
        "translatedText": "Mi nombre es John, nació en Nairobi y tengo 31 años de edad",
        "detectedSourceLanguage": "en"
      }
    ]
  }
}`

type data struct {
	Data struct {
		Translations []struct {
			TranslatedText string
			SourceLanguage string `json:"detectedSourceLanguage"`
		}
	}
}

func main() {
	var translation data
	err := json.Unmarshal([]byte(json_s), &translation)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", translation)
}

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

发表评论

匿名网友

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

确定