tags:

views:

29

answers:

1
+1  Q: 

JSON + GPathResult

Could i parse JSON string as GPathResult ? Or something with similar interface... I just have some code which doing some work with XML and i want to add JSON support. But GPathResult is very specific class and slightly differs form JSONObject or JSONArray.

+2  A: 

You can use JSONSlurper from json-lib:

@Grapes([
  @Grab(group='net.sf.json-lib', module='json-lib', version='2.3', classifier='jdk15')
])
import net.sf.json.groovy.*

def data = """{
  "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {"value": "New", "onclick": "CreateNewDoc()"},
        {"value": "Open", "onclick": "OpenDoc()"},
        {"value": "Close", "onclick": "CloseDoc()"}
      ]
    }
  }
}"""
def json = new JsonSlurper().parseText( data )

println json.menu.popup.menuitem*.value
tim_yates