GO JSON Data parsing

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

JSON is now a very common way of data passing, and go itself integrates the generation and parsing of JSON

Introduction Package

import {  "bytes"  "encoding/json"}

Analytical

    1. Define json the corresponding entity class, which is exactly the structure

      type GoAccessToken struct {  AccessToken string  ExpiresIn   int}
    2. Create a reference to the struct body

      goAccessToken := &GoAccessToken{}
    3. Unmarshalparse the string by going to json

      err := json.Unmarshal(str, goAccessToken)最终的 goAcessToken 为解析后的对象

If you find that the member variable in the object does not have a value, check that the first letter of the member variable is uppercase, and note that the key value of the JSON string is case-insensitive and only the first-capitalized member variable is parsed out of the value

Generate JSON

    1. Define json the corresponding entity class, which is exactly the structure

      type GoAccessToken struct {  AccessToken string  ExpiresIn   int}
    2. Create a reference to the struct and assign a value

      goAccessToken := &GoAccessToken{   "这是token值",   7200,}
    3. By Marshal going to generate a json string

      str, err := json.Marshal(goAccessToken)str 即为生成的 json 字符串

The first letter of the member variable must be capitalized, otherwise the variable will not be generated in the JSON string

Related Article

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.