In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Golang language JSON decoding function Unmarshal how to use", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Golang language JSON decoding function Unmarshal how to use it!"
Func Unmarshalfunc Unmarshal (data [] byte, v interface {}) error
The Unmarshal function parses the json-encoded data and stores the result in the value pointed to by v.
Unmarshal and Marshal do the opposite and apply for mapping, slicing or pointers if necessary, with the following additional rules:
To decode the json data to a pointer, the Unmarshal function first handles the case where the json data is the literal null of json. At this point, the function sets the pointer to nil;; otherwise, the function decodes the json data into the value pointed to by the pointer; if the pointer itself is nil, the function first requests a value and makes the pointer point to it.
To decode json data into a structure, the function matches the key of the input object with the key used by the Marshal (the structure field name or the key name specified by its label), preferring an exact match, but also accepts case-insensitive matches.
To decode json data to an interface type value, the function decodes the data into the following type to write to the interface:
Bool, for JSON booleans
Float64, for JSON numbers
String, for JSON strings
[] interface {}, for JSON arrays
Map[string] interface {}, for JSON objects
Nil for JSON null
If a JSON value does not match the given target type, or if a json number overflows when written to the target type, the Unmarshal function skips the field and tries to complete the rest of the decoding operation. If no more serious errors occur, this function returns a UnmarshalTypeError that describes the details of the first such error.
When the null value of JSON is decoded to the interface, pointer, and slice of go, they are set to nil, because null generally means "does not exist" in json. Decoding the null value of json to other go types does not cause any change or error.
When decoding a string, an illegal utf-8 or utf-16 proxy (character) pair is not considered an error, but replaces the illegal character with the unicode character U+FFFD.
Ordinary JSON
Sample code:
Package mainimport ("encoding/json"fmt") / / Actress actress type Actress struct {Name string Birthday string BirthPlace string Opus [] string} func main () {/ / normal JSON / / because the parameter received by the json.UnMarshal () function is a byte slice, / / so you need to convert the JSON string to a byte slice. JsonData: = [] byte (`{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City, Xinjiang", "opus": ["Anarhan", "Love against the Light", "Cara Lovers"]}`) var actress Actress err: = json.Unmarshal (jsonData) & actress) if err! = nil {fmt.Println ("error:", err) return} fmt.Printf ("name:% s\ n", actress.Name) fmt.Printf ("birthday:% s\ n", actress.Birthday) fmt.Printf ("place of birth:% s\ n", actress.BirthPlace) fmt.Println ("work:") for _, val: = range actress.Opus {fmt.Println ("\ t", val)}}
Running result:
Name: Dili Reba
Birthday: 1992-06-03
Place of birth: Urumqi, Xinjiang
Works:
"Anarhan."
"Love against the Light"
"carat lovers."
JSON embedded ordinary JSON
Sample code:
Package mainimport ("encoding/json"fmt") / / Opus works type Opus struct {Date string Title string} / / Actress actress type Actress struct {Name string Birthday string BirthPlace string Opus Opus} func main () {/ / JSON nesting ordinary JSON jsonData: = [] byte (`{"name": "Dili Reba", "birthday": "1992-06-03" "birthPlace": "Urumqi, Xinjiang", "opus": {"Date": "2013", "Title": "Anarhan"}} `) var actress Actress err: = json.Unmarshal (jsonData, & actress) if err! = nil {fmt.Println (" error: ", err) return} fmt.Printf (" name:% s\ n " Actress.Name) fmt.Printf ("birthday:% s\ n", actress.Birthday) fmt.Printf ("place of birth:% s\ n", actress.BirthPlace) fmt.Println ("work:") fmt.Printf ("\ t%s:%s", actress.Opus.Date, actress.Opus.Title)}
Running result:
Name: Dili Reba
Birthday: 1992-06-03
Place of birth: Urumqi, Xinjiang
Works:
2013: "Anarhan"
JSON embedded array JSON
Sample code:
Package mainimport ("encoding/json"fmt") type Opus struct {Date string Title string} type Actress struct {Name string Birthday string BirthPlace string Opus [] Opus} func main () {/ / JSON nested array JSON jsonData: = [] byte (`{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City, Xinjiang" "opus": [{"date": "2013", "title": "Anarhan"}, {"date": "2014", "title": "Love against the Light"}, {"date": "2015" "title": "carat lovers"}]} `) var actress Actress err: = json.Unmarshal (jsonData, & actress) if err! = nil {fmt.Println (" error: ", err) return} fmt.Printf (" name:% s\ n ", actress.Name) fmt.Printf (" birthday:% s\ n ", actress.Birthday) fmt.Printf (" place of birth:% s\ n " Actress.BirthPlace) fmt.Println ("works:") for _, val: = range actress.Opus {fmt.Printf ("\ t% s -% s\ n", val.Date, val.Title)}}
Running result:
Name: Dili Reba
Birthday: 1992-06-03
Place of birth: Urumqi, Xinjiang
Works:
2013-"Anarhan"
2014-"Love against the Light"
2015-"Carat Lovers"
JSON embedded JSON with dynamic Key
Sample code:
Package mainimport ("encoding/json"fmt") / / Opus works type Opus struct {Type string Title string} / / Actress actress type Actress struct {Name string Birthday string BirthPlace string Opus map [string] Opus} func main () {jsonData: = [] byte (`{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City, Xinjiang" "opus": {"2013": {"Type": "Modern Revolutionary Drama", "Title": "Anarhan"}, "2014": {"Type": "Fantasy", "Title": "Love against the Light"} "2015": {"Type": "Love drama", "Title": "Carat lovers"} `) var actress Actress err: = json.Unmarshal (jsonData, & actress) if err! = nil {fmt.Println (" error: ", err) return} fmt.Printf (" name:% s\ n " Actress.Name) fmt.Printf ("birthday:% s\ n", actress.Birthday) fmt.Printf ("place of birth:% s\ n", actress.BirthPlace) fmt.Println ("work:") for index, value: = range actress.Opus {fmt.Printf ("\ t date:% s\ n", index) fmt.Printf ("\ t\ t Category:% s\ n" Value.Type) fmt.Printf ("\ t\ t title:% s\ n", value.Title)}}
Running result:
Name: Dili Reba
Birthday: 1992-06-03
Place of birth: Urumqi, Xinjiang
Works:
Date: 2013
Classification: modern revolutionary drama
Title: "Anarhan"
Date: 2014
Category: fantasy dramas
Title: "Love against the Light"
Date: 2015
Category: love dramas
Title: "Cara Lovers"
Summary
We first introduced the Unmarshal function in the encoding/json package of the Golang standard library, and then introduced how to decode the following four kinds of JSON format data through the above four sample codes:
JSON format 1:
{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City, Xinjiang", "opus": ["Anarhan", "Love against the Light", "Kara Lovers"]}
JSON format 2:
{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City, Xinjiang", "opus": {"Date": "2013", "Title": "Anarhan"}}
JSON format 3:
{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City, Xinjiang", "opus": [{"date": "2013", "title": "Anarhan"}, {"date": "2014" "title": "Love against the Light"}, {"date": "2015", "title": "Carat Lovers"}]}
JSON format 4:
{"name": "Dili Reba", "birthday": "1992-06-03", "birthPlace": "Urumqi City of Xinjiang", "opus": {"2013": {"Type": "Modern Revolutionary Drama", "Title": "Anarhan"}, "2014": {"Type": "Fantasy" "Title": "Love against the Light"}, "2015": {"Type": "Love Drama", "Title": "Carat Lovers"}} so far I believe that the "Golang language JSON decoding function Unmarshal how to use" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.