drfqfuhej48511519 2018-04-17 23:19
浏览 77

GoLang从弹性搜索结果中解组JSON

I have data returned from Elasticsearch, using "github.com/olivere/elastic". That sort of works, when i add it to my struct and string it, like so,

data := Api {
    Total: myTotal,
    Data: string(result),
}

c.JSON(http.StatusOK, totalData)

the api is a struct like so,

type Api struct {
   Total interface{}
   Data interface{} 
}

This returns data ok, from 1 to any number of results on request. How the results loaded into the data interface are not escaped or something, e.g.

"Data":"{\"CID\":\"XXXXXXXXXX\",\"Link\":\"XXXXXXXXX\",

So I have tried to unmarshal the data before adding it to the api struct.

var p DataApi

err := json.Unmarshal(result, &p)
if err != nil {
    panic(err)
}

totalData := Api {
    Total: myTotal,
    Data: p,
}

c.JSON(http.StatusOK, totalData)

This sort of works fine, returns the data in the correct way, but only when loading one result. When 2 or more results are requested, I get this error from the unmarshal panic

invalid character '{' after top-level value

I have tried and google all over but can not find a solution to this? I am not sure what I am doing wrong? The DataApi is a nested set of structs, I was not sure if there was anything I should be being because of that?

This is being run within the Gin framework.

Thanks.

EDIT

So when I use fmt.Println on the string(result) I can print any number of results on the screen. How can I add this to the API struct and then I need the struct converted into JSON data. Is there some way of appending this string data on the JSON converted API struct?

  • 写回答

3条回答 默认 最新

  • doutian4046 2018-04-18 01:03
    关注

    Try to unmarshal multiple results into a slice:

    var q []Api
    err = json.Unmarshal(result, &q)
    

    See on playground https://play.golang.org/p/D_bVAd4jBlI

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type Api struct {
        Total interface{}
        Data  interface{}
    }
    
    func main() {
    
        data := Api{
            Total: 1,
            Data:  "2",
        }
    
        result, err := json.Marshal(data)
        if err != nil {
            panic(err)
        }
    
        fmt.Printf("single data: %s
    ", result)
    
        var p Api
        err = json.Unmarshal(result, &p)
        if err != nil {
            panic(err)
        }
    
        dataSlice := []Api{data}
    
        result, err = json.Marshal(dataSlice)
        if err != nil {
            panic(err)
        }
    
        fmt.Printf("slice of data: %s
    ", result)
    
        var q []Api
        err = json.Unmarshal(result, &q)
        if err != nil {
            panic(err)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题