Parsing json in Golang, constructing JSON

Source: Internet
Author: User

JSON parsing is the most indispensable aspect of today's (web) application development. Many languages require library support to parse and construct JSON, but Golang can do this well with native libraries.

There are two basic representations of JSON: struct and string. Analysis and construction is to solve string2struct and struct2string. In Golang, the library in charge of these two functions is located in "Encoding/json". The function usage instructions in the library can be viewed in "Https://gowalker.org/encoding/json". Below is a sample source for use.

JSON constructs:

Config. Host= "Http://www.baidu.com"

Config. port= "80"

If B,err:=json. Marshal (config); err==nil{
Fmt. Println (string (b))
}

JSON decoding

var jsonstr= ' {host ': ' http://www.baidu.com ', ' Port ': 80} '

If Err:=json. Unmarshal ([]byte (JSONSTR), &config); err==nil{
Fmt. Println (config)
}

Structure for storing JSON:

This point has some special, special instructions.

Type Configstruct struct{

Host string ' JSON: ' Host '

Port int ' JSON: ' Port '

}

Specifically, the member name associated with the JSON operation must start with a capital (that is, the lead), and add a string ' JSON: ' T (that is, name) after its type.

Parsing json in Golang, constructing JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.