Comments (4)
So you are referring to the
val := x.Get(obj)
call I assume. Using a JSONPath to retrieve values can match more than one value to the result must be some kind of slice. Since the retrieved values can be of any type, theinterface{}
type must be used to cover all those cases. So a slice an "any" is[]interface{}
. To retrieve a single value you can usex.First(obj)
which returns the first match as aninterface{}
. Returned value can aways be asserted to be the actual type so for example if the return fromx.First(obj)
is actually a string and that is expected then you could do something likestr, ok := x.First(obj).(string)
. In that case, if ok is true then str will be the string value. You could also use the OjGalt.String()
function if that suits your needs better.To answer your questions directly:
- The result of
Get()
is always a[]interface{}
as declared. It a feature of go that declaring the return type always results in that return type.- For a single value use
First()
instead. If a string is expected then assert as described above or usealt.String()
.- I wasn't sure what you were asking on this one. There really isn't anything to parse from the result. If you attempt to assert that a
[]interface{}
is an[]int64
that will fail at compile time. You can iterate over the result and assert that each element is an int64 though.
Thank you for your clearly answer!!! It completely solves my problem!
from ojg.
OjG has many functions. Can you tell me what function you are referring to?
from ojg.
Sorry, my fault.
here is my code:
obj ,_ := oj.ParseString(jsonString)
x, _ := jp.ParseString(jsonPath)
val := x.Get(obj)
from ojg.
So you are referring to the val := x.Get(obj)
call I assume. Using a JSONPath to retrieve values can match more than one value to the result must be some kind of slice. Since the retrieved values can be of any type, the interface{}
type must be used to cover all those cases. So a slice an "any" is []interface{}
. To retrieve a single value you can use x.First(obj)
which returns the first match as an interface{}
. Returned value can aways be asserted to be the actual type so for example if the return from x.First(obj)
is actually a string and that is expected then you could do something like str, ok := x.First(obj).(string)
. In that case, if ok is true then str will be the string value. You could also use the OjG alt.String()
function if that suits your needs better.
To answer your questions directly:
- The result of
Get()
is always a[]interface{}
as declared. It a feature of go that declaring the return type always results in that return type. - For a single value use
First()
instead. If a string is expected then assert as described above or usealt.String()
. - I wasn't sure what you were asking on this one. There really isn't anything to parse from the result. If you attempt to assert that a
[]interface{}
is an[]int64
that will fail at compile time. You can iterate over the result and assert that each element is an int64 though.
from ojg.
Related Issues (20)
- Remove nth element of an array using jsonPath HOT 8
- Sponsor this project HOT 4
- not support token contain '-' ? HOT 11
- Double value parse error on iOS platform HOT 6
- preserve order of JSONPath elements (when using wildcard) HOT 6
- Getting full path of a rule in the json HOT 1
- oj.Marshal fails on embedded interface HOT 2
- Extracting Multiple Fields HOT 2
- Array indexes with last return reverse order HOT 4
- Is it possible to use JSONPath to pick the objects without a particular field? HOT 4
- Working with a collection of JSONPaths simultaneously? HOT 2
- Does JsonPath supports escaping? HOT 13
- Option to keep order of keys HOT 16
- Does support ”-“ HOT 1
- Parse '1,2,3' should not succeed HOT 3
- Expr.String() doesn't escape strings HOT 6
- Question: Using jp.Set() to set non-existent slice indices HOT 2
- Maybe a regression on path filters on version 1.18.0 HOT 3
- support for "json" tag in struct elements HOT 4
- jp feature request: Set() that only replaces existing values HOT 27
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ojg.