From db0ab2f25522575ec96cd31671bffa49c7f322a1 Mon Sep 17 00:00:00 2001 From: ljun20160606 Date: Thu, 20 Dec 2018 16:11:18 +0800 Subject: [PATCH] fix: obj is map[interface{}]interface{}, but the key is string, $.key can lookup value --- jsonpath.go | 10 +++++++--- jsonpath_test.go | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/jsonpath.go b/jsonpath.go index 00dc6fd..9e84fef 100644 --- a/jsonpath.go +++ b/jsonpath.go @@ -347,10 +347,14 @@ func get_key(obj interface{}, key string) (interface{}, error) { } return val, nil } - for _, kv := range reflect.ValueOf(obj).MapKeys() { - //fmt.Println(kv.String()) + of := reflect.ValueOf(obj) + for _, kv := range of.MapKeys() { + // obj is map[interface{}]interface{}, but the key is string + if kv.Interface() == key { + return of.MapIndex(kv).Interface(), nil + } if kv.String() == key { - return reflect.ValueOf(obj).MapIndex(kv).Interface(), nil + return of.MapIndex(kv).Interface(), nil } } return nil, fmt.Errorf("key error: %s not found in object", key) diff --git a/jsonpath_test.go b/jsonpath_test.go index 90f05b7..512fda4 100644 --- a/jsonpath_test.go +++ b/jsonpath_test.go @@ -96,7 +96,7 @@ func Test_jsonpath_JsonPathLookup_1(t *testing.T) { if res_v, ok := res.([]interface{}); ok != true || res_v[0].(float64) != 8.95 || res_v[1].(float64) != 12.99 || res_v[2].(float64) != 8.99 || res_v[3].(float64) != 22.99 { t.Errorf("exp: [8.95, 12.99, 8.99, 22.99], got: %v", res) } - + // range res, err = JsonPathLookup(json_data, "$.store.book[0:1].price") t.Log(err, res)