Code Monkey home page Code Monkey logo

Comments (4)

zgsdr0620 avatar zgsdr0620 commented on June 13, 2024 1

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:

  1. 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.
  2. For a single value use First() instead. If a string is expected then assert as described above or use alt.String().
  3. 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.

ohler55 avatar ohler55 commented on June 13, 2024

OjG has many functions. Can you tell me what function you are referring to?

from ojg.

zgsdr0620 avatar zgsdr0620 commented on June 13, 2024

Sorry, my fault.

here is my code:

obj ,_ := oj.ParseString(jsonString)
x, _ := jp.ParseString(jsonPath)
val := x.Get(obj)

from ojg.

ohler55 avatar ohler55 commented on June 13, 2024

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:

  1. 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.
  2. For a single value use First() instead. If a string is expected then assert as described above or use alt.String().
  3. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.