dsf23223 2019-07-31 13:07
浏览 66

使用Go发布解析JSON文件

I make a GET request, and receive a JSON file, that I cannot parse.

Here is the data I have to parse

{
    "codeConv": "ACC00000321",
    "start": "2019-07-01T00:00:00Z",
    "end": "2019-08-21T00:00:00Z",
    "details": [
        {
            "idPrm": "30000000123456",
            "keys": [
                {
                    "timestamp": "2019-07-01T00:00:00Z",
                    "value": 0
                },
                {
                    "timestamp": "2019-07-01T00:30:00Z",
                    "value": 0
                },
                ...
            ]
        }, 
        {
            "idPrm": "30000000123457",
            "keys": [
                {
                    "timestamp": "2019-07-01T00:00:00Z",
                    "value": 1
                },
                {
                    "timestamp": "2019-07-01T00:30:00Z",
                    "value": 2
                },
                ...
            ]
        }
    ]
}

Here are my objects:

type APIMain struct {
    CodeConv string          `json:"codeConv"`
    Start    string          `json:"start"`
    End      []Keys          `json:"end"`
    Details  []APIData `json:"details"`
}

//APIData match the data we receive from api
type APIData struct {
    Prm  string `json:"idPrm"`
    Keys []Keys `json:"keys"`
}

type Keys struct {
    Timestamp string `json:"timestamp"`
    Value     string `json:"value"`
}

and here is the method to get data with basic auth:

tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    req, err := http.NewRequest("GET", url, nil)

    if err != nil {
        return nil, err
    }
    if login != "" && password != "" {
        req.SetBasicAuth(login, password)
    }

    response, err := client.Do(req)
    //defer response.Body.Close()
    if err != nil {
        return nil, err
    }
    if response.StatusCode != 200 {
        fmt.Println(response.Body)
        panic(response.Status)
    }

    err = json.NewDecoder(response.Body).Decode(&result)
    fmt.Println("result", result) // result is empty array

How can I see if the problem is in a request, or if the problem is in parsing ?

I have get a response.Body object, but it needs to be decoded.

  • 写回答

1条回答 默认 最新

  • douyanning3724 2019-07-31 13:31
    关注

    I fixed it using: https://mholt.github.io/json-to-go/

    which generated this structure:

    type AutoGenerated struct {
        CodeConv string    `json:"codeConv"`
        Start    time.Time `json:"start"`
        End      time.Time `json:"end"`
        Details  []struct {
            IDPrm string `json:"idPrm"`
            Keys  []struct {
                Timestamp time.Time `json:"timestamp"`
                Value     float64   `json:"value"`
            } `json:"keys"`
        } `json:"details"`
    }
    

    Thanks for your comments

    Great time saver !

    评论

报告相同问题?

悬赏问题

  • ¥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时遇到的编译问题