File: addr5.go

package info (click to toggle)
golang-github-traefik-yaegi 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,608 kB
  • sloc: sh: 457; makefile: 39
file content (62 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download | duplicates (2)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"encoding/json"
	"fmt"
	"net/url"
)

func main() {
	body := []byte(`{
		"BODY_1": "VALUE_1",
		"BODY_2": "VALUE_2",
		"BODY_3": null,
		"BODY_4": {
			"BODY_1": "VALUE_1",
			"BODY_2": "VALUE_2",
			"BODY_3": null
		},
		"BODY_5": [
			"VALUE_1",
			"VALUE_2",
			"VALUE_3"
		]
	}`)

	values := url.Values{}

	var rawData map[string]interface{}
	err := json.Unmarshal(body, &rawData)
	if err != nil {
		fmt.Println("can't parse body")
		return
	}

	for key, val := range rawData {
		switch val.(type) {
		case string, bool, float64:
			values.Add(key, fmt.Sprint(val))
		case nil:
			values.Add(key, "")
		case map[string]interface{}, []interface{}:
			jsonVal, err := json.Marshal(val)
			if err != nil {
				fmt.Println("can't encode json")
				return
			}
			values.Add(key, string(jsonVal))
		}
	}
	fmt.Println(values.Get("BODY_1"))
	fmt.Println(values.Get("BODY_2"))
	fmt.Println(values.Get("BODY_3"))
	fmt.Println(values.Get("BODY_4"))
	fmt.Println(values.Get("BODY_5"))
}

// Output:
// VALUE_1
// VALUE_2
// 
// {"BODY_1":"VALUE_1","BODY_2":"VALUE_2","BODY_3":null}
// ["VALUE_1","VALUE_2","VALUE_3"]