JSON processing for Golang

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

1. The relationship between string and []byte]

string and []byte can convert each other

s1 := "abcd"    b1 := []byte(s1)    fmt.Println(b1) // [97 98 99 100]    s2 := "中文"    b2 := []byte(s2)    

2. JSON string to JSON

var dat map[string]interface{}if err := json.Unmarshal([]byte(newstr), &dat); err == nil {fmt.Println(dat["info"])}

The JSON string to JSON is converted by the Unmarsha method, accepting the []byte parameter, so we can convert the string to []byte] by []byte[string] and place the JSON string into the map

3, why to use Goson

The JSON string is converted to map[string]interface{} We will find that we have no way to traverse a field in JSON for further processing unless we have defined the struct. And Goson offers us a solution.

Github.com/panthesingh/goson

4. Regular matching

result := util.HttpGet(onlineurl)reg := regexp.MustCompile(`\{.+\}`)strslince := reg.FindAllString(result, -1)newstr := strings.Join(strslince, "")

The string after the regular match is []string format is not string format, we want to convert []string to string to further convert to JSON, I use string. Join method, there should be a better way

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.