Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 746419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:05:20+00:00 2026-05-14T14:05:20+00:00

I’m converting a list of Foo objects to a JSON string. I need to

  • 0

I’m converting a list of Foo objects to a JSON string. I need to parse the JSON string back into a list of Foos. However in the following example, parsing gives me a list of JSONObjects instead of Foos.

Example

List list = [new Foo("first"), new Foo("second")]
def jsonString = (list as JSON).toString()

List parsedList = JSON.parse(jsonString) as List
println parsedList[0].getClass() // org.codehaus.groovy.grails.web.json.JSONObject

How can I parse it into Foos instead?
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:05:20+00:00Added an answer on May 14, 2026 at 2:05 pm

    I had a look at the API docs for JSON and there doesn’t appear to be any way to parse to a JSON string to a specific type of object.

    So you’ll just have to write the code yourself to convert each JSONObject to a Foo. Something like this should work:

    import grails.converters.JSON
    import org.codehaus.groovy.grails.web.json.*
    
    class Foo {
      def name
    
      Foo(name) {
        this.name = name
      }
    
      String toString() {
        name
      }
    }
    
    
    List list = [new Foo("first"), new Foo("second")]
    def jsonString = (list as JSON).toString()
    
    List parsedList = JSON.parse(jsonString)
    
    // Convert from a list of JSONObject to a list of Foo
    def foos = parsedList.collect {JSONObject jsonObject ->
        new Foo(name: jsonObject.get("name"))
    }
    

    A more general solution would be to add a new static parse method such as the following to the JSON metaClass, that tries to parse the JSON string to a List of objects of a particular type:

    import grails.converters.JSON
    import org.codehaus.groovy.grails.web.json.*
    
    class Foo {
      def name
    
      Foo(name) {
        this.name = name
      }
    
      String toString() {
        name
      }
    }
    
    List list = [new Foo("first"), new Foo("second")]
    def jsonString = (list as JSON).toString()
    
    
    List parsedList = JSON.parse(jsonString)
    
    // Define the new method
    JSON.metaClass.static.parse = {String json, Class clazz ->
    
        List jsonObjs = JSON.parse(json)
    
        jsonObjs.collect {JSONObject jsonObj ->
    
            // If the user hasn't provided a targetClass read the 'class' proprerty in the JSON to figure out which type to convert to
            def targetClass = clazz ?: jsonObj.get('class') as Class
            def targetInstance = targetClass.newInstance()        
    
            // Set the properties of targetInstance
            jsonObj.entrySet().each {entry ->
    
                if (entry.key != "class") {
                    targetInstance."$entry.key" = entry.value
                }
            }
            targetInstance
        }
    
    }
    
    // Try the new parse method
    List<Foo> foos = JSON.parse(jsonString, Foo)
    
    // Confirm it worked
    assert foos.every {Foo foo -> foo.class == Foo && foo.name in ['first', 'second'] }
    

    You can try out the code above in the groovy console. A few warnings

    • I have only performed very limited testing on the code above
    • There are two JSON classes in the latest Grails release, I’m assuming you’re using the one that is not deprecated
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 362k
  • Answers 362k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the standard Java HTTP operations. Check out… May 14, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer DOM stands for Document Object Model and, as you could… May 14, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer You should read this question: Javascript as a functional language… May 14, 2026 at 3:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.