Surbhi
Surbhi

Reputation: 3

Golang parsing into struct

I cannot parse this json into my structure. Can anyone please help with this

{"error":false,"response":{"results":[{"id":68876,"name":"cee lo green - big girls"},{"id":68954,"name":"charles, ray - the girl friend"},{"id":69603,"name":"charlie puth - la girls"},{"id":68001,"name":"city girls - careless"},{"id":68000,"name":"city girls - millionaire dick"},{"id":68002,"name":"city girls - period (we live)"},{"id":68004,"name":"city girls - rap shit"},{"id":68003,"name":"city girls - runnin"},{"id":68019,"name":"clairo - pretty girl"},{"id":68223,"name":"cohn, marc - girl of mysterious sorrow"},{"id":68343,"name":"contours, the - searching for a girl"}

Below is my struct package main

import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http")

type test struct{ SngID string json:"id" SngNm string json:"name" } type Inner struct{

Result[10] test `json:"results"`

}

type Outer struct{ Eror bool json:"error" Response [] Inner json:"results"

}

Upvotes: 0

Views: 564

Answers (1)

user14311453
user14311453

Reputation:

Your JSON is malformed (check the error returned by json.Unmarshal).

Anyway, this struct should work for you.

    type Response struct {
        Error    bool `json:"error"`
        Response struct {
            Results []struct {
                ID   int    `json:"id"`
                Name string `json:"name"`
            } `json:"results"`
        } `json:"response"`
    }

Upvotes: 2

Related Questions