JSON in Golang. Number Magical

Source: Internet
Author: User

Recently with a debugging API interface, interface return data is a json format, according to the document description is an integer data, so defined as follows

    type Data struct {        Api int `json:"api"`    }

In the case of the same entry , the first call, the result is:

{"api":1}

The second call, however, results in the following:

{"api":"1"}

Communication with the other developers to find this is a bug, due to process problems, no way to immediately modify the line, think or I do a good compatibility, the effect is both analytical and {"api":1} can be resolved {"api":"1"} , so I think, the number json.Number type is defined as follows

The representation of numbers is similar to that used in most
Programming languages. A number is represented in base using
Decimal digits. It contains an integer component
Prefixed with an optional minus sign, which May is followed by a
Fraction part and/or a exponent part. Leading zeros is not
Allowed.

Simple translation: Number can represent the decimal number, but the leading 0 data is not allowed (OK, this is not good, or look at the example) such as this 012 , and -012 so on
We re-adjust the data structure

    type Data struct {        Api json.Number `json:"api"`    }

Data parsing

    var d Data    err := json.Unmarshal([]byte(`{"api":"12"}`), &d)

In fact, the problem is that the data is definitely a decimal number, but it is not certain that there is no "" number, but json.Number it helps to abstract the concept of data, and in the premise of ensuring the type, it is up to the caller to decide the type of final use.

// String returns the literal text of the number.func (n Number) String() string { return string(n) }// Float64 returns the number as a float64.func (n Number) Float64() (float64, error) {    return strconv.ParseFloat(string(n), 64)}// Int64 returns the number as an int64.func (n Number) Int64() (int64, error) {    return strconv.ParseInt(string(n), 10, 64)}

I can use it safely.

    d.Api.Int64()

And get the type you want.
Of course, my problem can also be RawMessage solved by type

    type Data struct {        Api json.RawMessage `json:"api"`    }

The document is interpreted as follows:

// RawMessage is a raw encoded JSON value.// It implements Marshaler and Unmarshaler and can// be used to delay JSON decoding or precompute a JSON encoding.type RawMessage []byte

Determine the field name, get json the original data value inside, and then convert it to the type you want.
Often read the source code, often read new

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.