valyala / fastjson

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
MIT License
2.3k stars 138 forks source link

Keep getting error on GetString #79

Open buzzy opened 2 years ago

buzzy commented 2 years ago

According to the manual, there should be a function called GetString, but only GetStringBytes works for me.

var parser fastjson.Parser json, err := parser.Parse(string(response.Body))

data := json.GetString("data")

./main.go:368:31: json.GetString undefined (type *fastjson.Value has no field or method GetString)

Betelgeuse1 commented 1 year ago

You're using parser.Parse which return a *Value type. As defined here, *Value type doesn't define a GetString function, only GetStringBytes.

If you want to use GetString, use the top-level function → https://pkg.go.dev/github.com/valyala/fastjson#GetString Example from the pkg.go.dev documentation :

package main

import (
    "fmt"
    "github.com/valyala/fastjson"
)

func main() {
    data := []byte(`{"foo":{"bar":[123,"baz"]}}`)

    s := fastjson.GetString(data, "foo", "bar", "1")
    fmt.Printf("data.foo.bar[1] = %s", s)
}