23

I have the following string from a REST JSON response:

[
   {
      "uid":10512213,
      "name":"Bob"
   },
   {
      "uid":7208201,
      "name":"John"
   },
   {
      "uid":10570,
      "name":"Jim"
   },
   {
      "uid":1799657,
      "name":"Sally"
   }
]

The rest response definition is from Facebook: FB REST Link

I am using Google App Engine + GAELYK which runs in Jetty.

What is the best way to convert the above into array of maps in Groovy on the Server. (This would probably have to recurse through the response)

I am looking for something easy that doesn't include a lot of libraries. (I dont have maven)

Rao
  • 20,781
  • 11
  • 57
  • 77
Tihom
  • 3,384
  • 6
  • 36
  • 47
  • Go to http://json.org/ and pick a class that will meet your needs. – James Black Oct 04 '10 at 12:36
  • I tried using JSONObject but that failed on the first character. It didn't like [ . I think I need to convert the string into an Array first and then use JSONObject on each individual item from json.org. – Tihom Oct 04 '10 at 12:43

2 Answers2

52

EDIT: Groovy since 1.8.0 has an integrated JsonSlurper:

import groovy.json.JsonSlurper

// Example Response Data
def restResponse = '[{"uid":10512213, "name":"Bob"},{"uid":7208201, "name":"John"},{"uid":10570, "name":"Jim"},{"uid":1799657, "name":"Sally"}]'

// Parse the response
def list = new JsonSlurper().parseText( restResponse )

// Print them out to make sure
list.each { println it }

Old answer below:

Use JsonSlurper...

An example script to read that response would be:

@Grab('net.sf.json-lib:json-lib:2.3:jdk15')
import net.sf.json.groovy.JsonSlurper

// Example Response Data
def restResponse = '[{"uid":10512213, "name":"Bob"},{"uid":7208201, "name":"John"},{"uid":10570, "name":"Jim"},{"uid":1799657, "name":"Sally"}]'

// Parse the response
def list = new JsonSlurper().parseText( restResponse )

// Print them out to make sure
list.each { println it }

This outputs:

[uid:10512213, name:Bob]
[uid:7208201, name:John]
[uid:10570, name:Jim]
[uid:1799657, name:Sally]

As you can see, list is a list of Maps, so if you just wanted a list of the names for example, you could just do:

def names = list.name

To use this in your Gaelyk app, you should just need to download json-lib-2.3-jdk15.jar from here and do something similar (without the @Grab then, as you'll have the jar in your WEB-INF/lib folder.

--edit--

Looking around, found this page showing the dependencies for json-lib

The @Grab in the test script does a lot of background work for you

tim_yates
  • 167,322
  • 27
  • 342
  • 338
8

JSON Arrays starts with a [ character and ends with a ] character. JSON object starts with a { and ends with }.

If you go to JSON.org, you can download JSONArray.java. Use that to create a JSON array. You then loop through the array for (int i = 0; i < array.length(); i++) and retrieve each JSON object by calling array.getJSONObject(i); which returns JSONObject. From there, get the respective attribute value, e.g. long uid = json.getLong("uid");

Hope this helps.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228