-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson_array.go
More file actions
69 lines (57 loc) · 1.99 KB
/
json_array.go
File metadata and controls
69 lines (57 loc) · 1.99 KB
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
63
64
65
66
67
68
69
package simplejson
import "encoding/json"
//JSONArray is a struct that represents an Array of JSON-compatible types and provides methods to access them
type JSONArray struct{
innerArray [] interface{}
}
// NewJSONArrayFromString returns a new JSONArray, parsed from a string or an error if unsuccessful
func NewJSONArrayFromString(jsonarray string) (*JSONArray, error) {
var resultingArray []interface{}
err := json.Unmarshal([]byte(jsonarray), &resultingArray)
if err != nil {
return nil, err
}
return &JSONArray{resultingArray}, nil
}
func NewJSONArrayWithArray(array []interface{}) (*JSONArray, bool) {
resultingArray, ok := interfaceToJsonCompatible(array)
if !ok {
return nil, false
}
castedArray := resultingArray.([]interface{})
return &JSONArray{castedArray}, true
}
// JSONArray returns JSONArray from specific index
func (this *JSONArray) JSONArray(index int) *JSONArray {
return &JSONArray{this.innerArray[index].([]interface{})}
}
// JSONObject returns JSONArray from specific index
func (this *JSONArray) JSONObject(index int) *JSONObject {
return &JSONObject{this.innerArray[index].(map[string]interface{})}
}
// String returns String from specific index
func (this *JSONArray) String(index int) string {
return this.innerArray[index].(string)
}
// Bool returns bool from specific index
func (this *JSONArray) Bool(index int) bool {
return this.innerArray[index].(bool)
}
// Int returns int from specific index
func (this *JSONArray) Int(index int) int {
return parseInt(this.innerArray[index])
}
// Float32 returns float32 from specific index
func (this *JSONArray) Float32(index int) float32 {
float64Representation := float32(this.innerArray[index].(float64))
return float64Representation
}
// Float64 returns float64 from specific index
func (this *JSONArray) Float64(index int) float64 {
return this.innerArray[index].(float64)
}
// Length returns the length of this JSONArray
func (this *JSONArray) Length() int {
theInnerArray := this.innerArray;
return len(theInnerArray)
}