Go processing JSON format files

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Focus on open source China for a long time, but has not published a blog, the reason is to feel that their ability is not enough, because the people here let me look up ah. has been in the csdn adhere to, but, recently, but always feel that the atmosphere is not right, then came here to express their views, as well as their own study notes.

Work is to use the go language, in fact, their own learning is not very deep, but barely can be used to make a few lines of code, out of the problem or with the API to help, had conceived the past to read the source, but has not been determined, fortunately there are many testcase, provided the method of calling the function.

Took a long time, there is always no way to decode my definition of the file, the struct changed countless times, run testcase is really correct, one to my program will not be. When I was about to give up, I suddenly noticed one thing, the key in the example was in uppercase, and I had a sense of a sudden sensation. After all, in order to be concise, go saves a lot of keywords, such as public,private, such as keywords are case-sensitive, so, in go, the case should have an unusual meaning. Sure enough, the testcase in the upper case to lowercase, while the definition of uppercase to lowercase, indeed, not read out, really, wasted my afternoon time to see all the testcase json.

In addition to the above, you have to capitalize the key in the field, and there is a problem that deserves attention. If you think of yourself as this method, how do you interpret it? Of course, to parse according to the structure, because the JSON format of the data may not be fixed, perhaps in some data items, some key position is not fixed, then how to match, through the variable name, that is, when specifying this structure, each property name is defined as the same as the key name in the JSON. (The above is my guess, did not read the source) for example,

[{' name ': ' Alias ', ' Age ': 11},{' age ': ' name ': ' Lucy '}]

If you want to define the same structure,

Typre Student struct{

Name string

Age int

}

Here, either the definition of JSON data or the definition in a struct, there is no way to organize data according to our defined structure area in the case of the first letter lowercase.

Of course, we can also use nested, for example:

{"Age": One, "Name": "Jessica", "Course": {"Eng": $, "Math": 90}}

The definition of our structure can be adjusted to:

Type score struct{

Eng int

Math int

}

Type Scorelist struct{

Name string

Age int

Course *score

}

For more than one case, using an array is possible. Once the struct is defined, the Unmarshal method that calls the JSON directly is OK.

The first parameter is []byte, the JSON data needs to be converted to a byte array, and the second parameter is the structure we define.

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.