Mutual transformation of JSON, map, and struct in Go_14:golang

Source: Internet
Author: User

1. JSON to struct in Golang

  <1. Use JSON. When Unmarshal, each item of the struct must be an export item (import field). That is, the key corresponding to the structure must be capitalized in the first letter. Take a look at the following example:

Package Commontestimport ("Testing"    "Encoding/json") Type personstruct{Namestring Ageint}func Teststruct2json (t*testing. T) {jsonstr:= `    {        "name":"liangyongxing",        " Age": A} ' var person json. Unmarshal ([]byte(JSONSTR), &Person ) T.Log (person)}

The results of the output are as follows:

{0}

as you can see from the results, the JSON data is not written to the person structure body. Structure key first uppercase words can be, modified:

Package Commontestimport ("Testing"    "Encoding/json") Type personstruct{ Name string Ageint}func Teststruct2json (t*testing. T) {jsonstr:= `    {        "name":"liangyongxing",        " Age": A} ' var person json. Unmarshal ([]byte(JSONSTR), &Person ) T.Log (person)}

The printing results are as follows:

{liangyongxing 12}

from the above results we can find a very important message, the key in the JSON and the struct inside the key is a lowercase one is uppercase, that is, the case is not on the right. From here we can conclude that to be able to attach a value requires the initial capitalization of the variable name in the struct, and the case in the converted JSON string can be, that is, the JSON crosses the field name case insensitive. Then it is verified that the JSON is written in the following way:

JSONSTR: = '    {        'NaMe':'liangyongxing ' ,        "Age":    

  The end result is still valuable, so it validates our conclusion that the JSON string is insensitive to the case of the field name (not necessarily the initial letter, which requires attention )

<2. Tag tags can be introduced in the structure, so that when matching the JSON string corresponding field names need to match the field name defined in the tag tag, of course, the name defined in tag does not need to capitalize the first letter, and the corresponding JSON string field names are still case insensitive, and the above conclusion is consistent. ( Note: the corresponding field names in the struct are not identical to the match, but must also be capitalized, only uppercase ones are available for external access)

Package Commontestimport ("Testing"    "Encoding/json")//the corresponding n and a can not be lowercase, the first letter must be uppercase, so as to provide access to the outside, the specific JSON match is through the following tag tag to match, and N and A is not related//in the tag tag, the JSON is followed by the field name, which is a string type that requires double quotes, otherwise Golang is a type that does not recognize it.Type personstruct{Nstring' JSON:"name"' Aint' JSON:" Age"'}func Teststruct2json (t*testing. T) {jsonstr:= `    {        "name":"liangyongxing",        " Age": A} ' var person json. Unmarshal ([]byte(JSONSTR), &Person ) T.Log (person)}

The result of this output is as follows:

{liangyongxing 12}

Of course, you can also do another experiment, verify that the corresponding field name in the tag tag is not sensitive to the case, there is no redundancy introduced here.

2. Golang in the struct to JSON string

  In a continuous summary

Mutual transformation of JSON, map, and struct in Go_14:golang

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.