0

I created a JSON object via JSONBuilder class:

def json = new JSONBuilder().build {...}

How can I transform this JSON object to groovy object, so that I can access json properties via dot('.') operator? JSON object has a field - target. And I can access properties:

target.someProperty

But target property is protected. Is there any other way to access json properties?

P.S. Currently I use work around:

new JsonSlurper().parseText(json.toString())

It works, but I want something simplier.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Korobko Alex
  • 816
  • 1
  • 7
  • 10

1 Answers1

0

You could do something like this:

    JSONObject jsonObject = JSON.parse(json.toString()) as JSONObject

    println jsonObject.validproperty

    MyObject myObject = new MyObject(properties:jsonObject.properties)

This is largely dependent (obviously) on what the mapping is between your JSONObject and MyObject, but it should work.

Nathan Dunn
  • 447
  • 5
  • 16