elasticsearch,unmarshalling,Json,Go,elasticsearch,Unmarshalling" /> elasticsearch,unmarshalling,Json,Go,elasticsearch,Unmarshalling" />

在Go中像JSON一样解组表

在Go中像JSON一样解组表,json,go,elasticsearch,unmarshalling,Json,Go,elasticsearch,Unmarshalling,ElasticSearch以JSON格式的类似表格的结构返回结果(列描述,然后记录为数组)。例如: { "columns" : [ { "name" : "a", "type" : "text" }, { "name" : "b", "type" : "

ElasticSearch以JSON格式的类似表格的结构返回结果(列描述,然后记录为数组)。例如:

{
  "columns" : [
    {
      "name" : "a",
      "type" : "text"
    },
    {
      "name" : "b",
      "type" : "text"
    }
  ],
  "rows" : [
    [
      "a1",
      "b1"
    ],
    [
      "a2",
      "b2"
    ]
  ],
  "cursor" : "Qmd9STC/hM/p3fEF9F7D7FehVn+yLHX4SGVcAB7vk9kGwLjZ4wgfQex4gH+9PuSRmweeHMKtkFiUbTRNFC9bbse4zszAaMv9zKG11aEXQzJzjlRuypdHyDA+RPz66xPVuI1UUkoFpw5EY7k8bFKQ31zhn7x0ie0gv4jGseJJetzXNugw8TwYNR6Id0MVSihm0ogRH9WNFA72CJnqoa26zDPIgXSm/D6QPP40/yXozyAE0gzMnFUYynZf1vlCVdHTQPrCo0TrSMlHvx3BPza5ZnzyLEYZLKULRrTUvtiMOxj+5Ru4izLWSB0jLqeTEkbl5OK9Tyniuq45PeZmZ39UBQ=="
}
是否有最佳做法将其解组到Go结构数组(具有有意义字段名的结构)中。例如:

这行吗

package main

import (
   "encoding/json"
   "fmt"
)

const s = `
{
   "rows" : [
      ["a1", "b1"], ["a2", "b2"]
   ]
}
`

type platipus struct { A, B string }

func (p *platipus) UnmarshalJSON(b []byte) error {
   a := []*string{&p.A, &p.B}
   return json.Unmarshal(b, &a)
}

func main() {
   var p struct {
      Rows []platipus
   }
   json.Unmarshal([]byte(s), &p)
   fmt.Printf("%+v\n", p.Rows) // [{A:a1 B:b1} {A:a2 B:b2}]
}
这行吗

package main

import (
   "encoding/json"
   "fmt"
)

const s = `
{
   "rows" : [
      ["a1", "b1"], ["a2", "b2"]
   ]
}
`

type platipus struct { A, B string }

func (p *platipus) UnmarshalJSON(b []byte) error {
   a := []*string{&p.A, &p.B}
   return json.Unmarshal(b, &a)
}

func main() {
   var p struct {
      Rows []platipus
   }
   json.Unmarshal([]byte(s), &p)
   fmt.Printf("%+v\n", p.Rows) // [{A:a1 B:b1} {A:a2 B:b2}]
}