Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze simplejson with golang source code

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report