This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Pages: 1 - Last Post: 30 Aug 24, 10:47 Last Post By: davidekholm Threads: [ Previous | Next ]
wiener30

Posts: 17
Registered: 8-Sep-2009
How to parse json string in groovy ?
Posted: 29 Aug 24, 11:44
 
  Click to reply to this thread Reply
In seems the library
import groovy.json.JsonSlurper

is not available.
RobM

Posts: 3,964
Registered: 4-Aug-2006
Re: How to parse json string in groovy ?
Posted: 29 Aug 24, 12:26   in response to: wiener30 in response to: wiener30
 
  Click to reply to this thread Reply
wiener30

Posts: 17
Registered: 8-Sep-2009
Re: How to parse json string in groovy ?
Posted: 29 Aug 24, 12:33   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
I have a string
def jsonString = '''{
    "geoObjects": [
        {
            "name": "Summit 1",
            "latlon": "36.12345, 6.123456",
        },
        {
            "name": "Summit 2",
            "latlon": "36.6789, 6.6789",
        },
    ]
}'''

and I want to extract it into an Array of Maps.
davidekholm

Posts: 3,666
Registered: 18-Oct-2002
Re: How to parse json string in groovy ?
Posted: 29 Aug 24, 21:48   in response to: wiener30 in response to: wiener30
 
  Click to reply to this thread Reply
jAlbum uses a json library called json-simple. Here's a usage example. Try it in jAlbum's system console:
import com.github.cliftonlabs.json_simple.*;
 
jsonString = '''{
    "geoObjects": [
        {
            "name": "Summit 1",
            "latlon": "36.12345, 6.123456",
        },
        {
            "name": "Summit 2",
            "latlon": "36.6789, 6.6789",
        },
    ]
}'''
 
data  = Jsoner.deserialize(jsonString)
 
data.geoObjects[0].name // Prints "Summit 1"
data.geoObjects[0].latlon  // Prints "36.12345, 6.123456"
RobM

Posts: 3,964
Registered: 4-Aug-2006
Re: How to parse json string in groovy ?
Posted: 29 Aug 24, 22:00   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
jAlbum uses a json library called json-simple. Here's a usage example.
That's interesting and good to know, I've added it to the code snippets page for easy access.
Never used json in pure Groovy, always converted it to JavaScript to make the html pages.
wiener30

Posts: 17
Registered: 8-Sep-2009
Re: How to parse json string in groovy ?
Posted: 30 Aug 24, 08:15   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Thank you, very helpful.
davidekholm

Posts: 3,666
Registered: 18-Oct-2002
Re: How to parse json string in groovy ?
Posted: 30 Aug 24, 10:47   in response to: wiener30 in response to: wiener30
 
  Click to reply to this thread Reply
You're welcome!
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums