How convert a string into json or a struct?

Hey
I am very new with go i am developing a simple bot that works with httprequest
How i can parse a string like this
:string{“error”:[],“result”:{“KFEE”:{“aclass”:“currency”,“altname”:“FEE”,“decimals”:2,“display_decimals”:2},“XDAO”:

in something that i coudl manage better to work… for example struct or json…

I hope someone coudl help me

If the string did not start with “:string”, it would be a valid JSON string, and you could decode it with the json.Unmarshal function.

From your other post (that you erroneously posted into the “Jobs” subforum, BTW), I conclude you want to access the Web API of kraken.com and parse the results. The API returns JSON data, so you can use standard JSON decoding to parse this data into a struct.

Helpful resources:

The Go Blog about JSON:
https://blog.golang.org/json-and-go

JSON in Go By Example:
https://gobyexample.com/json

And this one is a really helpful tool: Paste some JSON and get the required Go structs back.
https://mholt.github.io/json-to-go/
(From the creator of Caddyserver.)

HTH

3 Likes

ok thank you for your answear and sorry for the redundance post but i was very desperated!!
but I still dont undestand how i can do…

this is my code right now

func main() {
	resp, err := http.Get("https://api.kraken.com/0/public/Time")
	if err != nil {
		// handle error
	}
	bs, err := ioutil.ReadAll(resp.Body)
	tr := string(bs)
	fmt.Println(tr)
}

the output of the println is this string:
{“error”:[],“result”:{“unixtime”:1474190201,“rfc1123”:“Sun, 18 Sep 16 09:16:41 +0000”}}

how i could work with this???

ps : I am very sorry for dont undestand with the first message but i am very new in GOlang is just 6 days

Hey @emanuelecesari, you probably want something like this:

package main

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

type Result struct {
	Unixtime int    `json:"unixtime"`
	Format   string `json:"rfc1123"`
}

type Time struct {
	Error  []string `json:"error"`
	Result `json:"result"`
}

func main() {
	resp, err := http.Get("https://api.kraken.com/0/public/Time")
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()

	t := new(Time)
	if err := json.NewDecoder(resp.Body).Decode(t); err != nil {
		log.Fatalln(err)
	}

	if len(t.Error) > 0 {
		log.Fatalln(t.Error)
	}

	fmt.Printf("Format: %s\nUnixtime: %d\n", t.Format, t.Unixtime)
}

Edit: You could also embed the Result struct directly inside of the Time struct like the following if you wanted to, instead of using composition: https://play.golang.org/p/AE_PpYWsYt.

2 Likes

Thank you for your answear I resolve my problem using the json unmarshal function

type Time struct {
	Error  []string `json:"error"`
	Result struct {
		Unixtime int    `json:"unixtime"`
		Format   string `json:"rfc1123"`
	} `json:"result"`
}

func main() {
	resp, err := http.Get("https://api.kraken.com/0/public/Time")
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()

	t := new(Time)
	bs, _ := ioutil.ReadAll(resp.Body)
	json.Unmarshal(bs, &t)

	if len(t.Error) > 0 {
		log.Fatalln(t.Error)
	}
	println(t.Result.Format)

}

what do you think? which is better? I really want know what do you think because I am very new in go!

In this case I would use the json.NewDecoder.Decode(t) since it will be reading directly from resp.Body.

In general if you are going to decode directly from an io.Reader, I would use json.NewDecoder.Decode but if you already have the bytes for what you want to use to decode, then use json.Unmarshal.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.