forked from mirror/gjson
parent
b95abbe94a
commit
495633298f
5
gjson.go
5
gjson.go
|
@ -138,10 +138,11 @@ func (t Result) Float() float64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array returns back an array of children. The result must be a JSON array.
|
// Array returns back an array of children. If the result is not
|
||||||
|
// a JSON array, the return will be an array containing one result.
|
||||||
func (t Result) Array() []Result {
|
func (t Result) Array() []Result {
|
||||||
if t.Type != JSON {
|
if t.Type != JSON {
|
||||||
return nil
|
return []Result{t}
|
||||||
}
|
}
|
||||||
r := t.arrayOrMap('[', false)
|
r := t.arrayOrMap('[', false)
|
||||||
return r.a
|
return r.a
|
||||||
|
|
|
@ -445,6 +445,18 @@ func TestUnmarshalMap(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSingleArrayValue(t *testing.T) {
|
||||||
|
var json = `{"key": "value"}`
|
||||||
|
var result = Get(json, "key")
|
||||||
|
var array = result.Array()
|
||||||
|
if len(array) != 1 {
|
||||||
|
t.Fatal("array is empty")
|
||||||
|
}
|
||||||
|
if array[0].String() != "value" {
|
||||||
|
t.Fatal("got %s, should be %s", array[0].String(), "value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type BenchStruct struct {
|
type BenchStruct struct {
|
||||||
Widget struct {
|
Widget struct {
|
||||||
Window struct {
|
Window struct {
|
||||||
|
|
Loading…
Reference in New Issue