Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze simplejson with golang source code

Shulou Source: shulou.com Published: 2022-06-01 14:00:38 09月27日 Update

How to use golang source code to analyze simplejson, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Background:

1 encoding/json, the json parsing library that comes with json Golang, provides the conversion between json strings and json objects, which is easy to use when the json string is relatively simple, but when the json string is more complex or more nested, it seems inadequate, and it is impossible to use encoding/json to define a struct type for each nested field. In this case, using the simplejson library can be very convenient for parsing.

2. When the parsed json data is not necessarily complete, parsing will often fail using the standard library, but parsing part of the data is also acceptable, so we can use simplejson

Source code

As you can see, the basic idea is to parse the data into an interface {}, and then do type inference.

The bottom layer is still a standard library.

Func NewJson (body [] byte) (* Json, error) {j: = new (Json) err: = j.UnmarshalJSON (body)} func (j * Json) UnmarshalJSON (p [] byte) error {dec: = json.NewDecoder (bytes.NewBuffer (p) dec.UseNumber () return dec.Decode (& j.data)} func (j * Json) Map () (map [string] interface {}, error) {if m Ok: = (j.data). (map [string] interface {}) Ok {return m, nil func (j * Json) Array () ([] interface {}, error) {if a, ok: = (j.data). ([] interface {}); ok {return a, niljson.Decoder vs json.Unmarshal

There are two ways to deserialize son:

Use json.Unmarshal passing the entire response string

/ / func Unmarshal (data [] byte, v interface {}) error

Data, err: = ioutil.ReadAll (resp.Body)

If err = = nil & & data! = nil {

Err = json.Unmarshal (data, value)

}

Using json.NewDecoder.Decode

/ / func NewDecoder (r io.Reader) * Decoder

/ / func (dec * Decoder) Decode (v interface {}) error

Err = json.NewDecoder (resp.Body) .Decode (value)

The two methods seem to be similar, but they have different application scenarios.

Use json.Decoder if your data is coming from an io.Reader stream, or you need to decode multiple values from a stream of data.

For the case of reading from an HTTP request, irreducd pick json.Decoder since you're obviously reading from a stream.

Use json.Unmarshal if you already have the JSON data in memory.

Read a huge json array from a file using json.Decoder

Json.Decoder will load one element at a time and will not read the entire json array into memory.

Read the json stream from the file with json.Decode to read the above content with [] byte in memory with json.Unmarshal. Have you mastered how to analyze simplejson with golang source code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Source code characters strings data methods analysis memory content arrays files methods time more standard type or problem different complex huge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS MariaDB MySQL NVidia