Golang a JSON conversion problem for a relatively easy-to-get-caught pit uint8[] array of JSON libraries.

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

Similar to this CA, if the instance is converted to json.list it becomes a string.

[CPP]View Plaincopyprint?
    1. type ca  struct  {  
    2.     list [] uint8  
    3. }  
    4.   
    5. Func main ()  {  
    6.      ca := ca{[]uint8{1,2,3,4,5,6,7,8,9,0}}  
    7.     r, _ := json. Marshal (CA)   
    8.     fmt. Println (String (r))  //{"List": "aqidbaugbwgjaa=="}   
    9. }  
Type CA struct {List []uint8}func Main () {CA: = Ca{[]uint8{1,2,3,4,5,6,7,8,9,0}}r, _: = json. Marshal (CA) fmt. Println (String (r))//{"List": "aqidbaugbwgjaa=="}}

And what we want is a normal JSON. Only int8 byte uint8 this 8-bit JSON library will treat us as strings. Want to solve. If you shoot your head, you can't use these types. 16 32 64 digits are required to convert to JSON strings normally.

[CPP]View Plaincopyprint?
    1. {"List": [1,2,3,4,5,6,7,8,9,0]}
{"List": [1,2,3,4,5,6,7,8,9,0]}


But don't worry. Unmarshal the JSON string that we think we don't want to go back to the other instances of the CA will get the desired result.

If it's a cross-language JSON communication, be careful.

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.