Here's an example of how you can parse JSONP (JSON with padding) string in Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
func main() {
// JSONP string
jsonp := []byte(`callbackFunction({"message": "Hello, World!"})`)
// Get the position of the first '(' and the last ')'
start := bytes.IndexByte(jsonp, '(')
end := bytes.LastIndexByte(jsonp, ')')
// Extract the JSON string from the JSONP string
jsonString := jsonp[start+1 : end]
// Unmarshal the JSON string into a Go map
var data map[string]interface{}
json.Unmarshal(jsonString, &data)
// Print the message
fmt.Println(data["message"])
}
In this example, we first extract the JSON string from the JSONP string by finding the positions of the first '('
and the last ')'
. We then use the json.Unmarshal
function to unmarshal the JSON string into a Go map. Finally, we print the "message"
field of the map.