英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论