PaesslerAG / jsonpath

BSD 3-Clause "New" or "Revised" License
172 stars 37 forks source link

can't deal some strings with numbers and special chars!!!!! #19

Open liuhongda11 opened 4 years ago

liuhongda11 commented 4 years ago

`v := interface{}(nil)

json.Unmarshal([]byte(`{
    "welcome":{
            "123456":["Good Morning", "Hello World!"]
        }
    }`), &v)

welcome, err := jsonpath.Get("$.welcome.123456[1]", v)`
liuhongda11 commented 4 years ago

`v := interface{}(nil)

json.Unmarshal([]byte(`{
    "welcome":{
            "product-dv":["Good Morning", "Hello World!"]
        }
    }`), &v)

welcome, err := jsonpath.Get("$.welcome.product-dv[1]", v)`
BorisKozo commented 4 years ago

I have encountered this and other issues as well.

remnestal commented 4 years ago

@liuhongda11 For the first example, jsonpath.Get("$.welcome.123456[1]", v), I would propose you use bracket-notation in your path, such as:

jsonpath.Get(`$.welcome["123456"][1]`, v)

I haven't attempted with a hyphen in the index, but I'm guessing that can be solved using bracket-notation as well. Note that I use double-quotes (") in the path above. When using single quotes (') it seems like the number is treated as a character and thus won't work for numbers with more than one digit.