Debian Codegraph @

_test/addr5.go

raw symbols SCIP
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7 )
8
9 func main() {
10 body := []byte(`{
11 "BODY_1": "VALUE_1",
12 "BODY_2": "VALUE_2",
13 "BODY_3": null,
14 "BODY_4": {
15 "BODY_1": "VALUE_1",
16 "BODY_2": "VALUE_2",
17 "BODY_3": null
18 },
19 "BODY_5": [
20 "VALUE_1",
21 "VALUE_2",
22 "VALUE_3"
23 ]
24 }`)
25
26 values := url.Values{}
27
28 var rawData map[string]interface{}
29 err := json.Unmarshal(body, &rawData)
30 if err != nil {
31 fmt.Println("can't parse body")
32 return
33 }
34
35 for key, val := range rawData {
36 switch val.(type) {
37 case string, bool, float64:
38 values.Add(key, fmt.Sprint(val))
39 case nil:
40 values.Add(key, "")
41 case map[string]interface{}, []interface{}:
42 jsonVal, err := json.Marshal(val)
43 if err != nil {
44 fmt.Println("can't encode json")
45 return
46 }
47 values.Add(key, string(jsonVal))
48 }
49 }
50 fmt.Println(values.Get("BODY_1"))
51 fmt.Println(values.Get("BODY_2"))
52 fmt.Println(values.Get("BODY_3"))
53 fmt.Println(values.Get("BODY_4"))
54 fmt.Println(values.Get("BODY_5"))
55 }
56
57 // Output:
58 // VALUE_1
59 // VALUE_2
60 //
61 // {"BODY_1":"VALUE_1","BODY_2":"VALUE_2","BODY_3":null}
62 // ["VALUE_1","VALUE_2","VALUE_3"]