func main() {
artist := "Madonna"
token := "5936fb55e90cdd9938f8e7086c783c40"
url := fmt.Sprintf(
"http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=%s&api_key=%s&format=json", artist, token)
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
data, err := json.MarshalIndent(string(body), "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
artist := "Madonna"
token := "5936fb55e90cdd9938f8e7086c783c40"
url := fmt.Sprintf(
"http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=%s&api_key=%s&format=json", artist, token)
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
var pretty bytes.Buffer
err = json.Indent(&pretty, body, "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", string(pretty.Bytes()))
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
artist := "Madonna"
token := "5936fb55e90cdd9938f8e7086c783c40"
url := fmt.Sprintf(
"http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=%s&api_key=%s&format=json", artist, token)
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var data map[string]interface{}
// десериализуете JSON
err = json.Unmarshal(body, &data)
if err != nil {
log.Fatal(err)
}
// сериализуете данные в JSON
marshaledDataBytes, err := json.MarshalIndent(data, "", "\t")
fmt.Println(string(marshaledDataBytes))
}