import groovyx.net.http.RESTClient;
def twitter = new RESTClient('http://search.twitter.com/')
def response = twitter.get(path:'search.json', query:[q:'dog'])
response.data.results.each {
it.each { key, value ->
println "$key = $value"
}
}
However a problem arises when the value is 'null'.
e.g.
{
results: [
{
text: "some text",
to_user_id: null, <-- null value
from_user: "exampleuser",
...
}
]
...
}
When run this script outputs:
text = some text
Exception in thread "main" net.sf.json.JSONException: null object
at net.sf.json.JSONObject.verifyIsNull(JSONObject.java:2856)
at net.sf.json.JSONObject.isEmpty(JSONObject.java:2212)
at org.codehaus.groovy.runtime.InvokerHelper.formatMap(InvokerHelper.java:544)
at org.codehaus.groovy.runtime.InvokerHelper.toMapString(InvokerHelper.java:610)
at org.codehaus.groovy.runtime.InvokerHelper.write(InvokerHelper.java:467)
at groovy.lang.GString.writeTo(GString.java:188)
at groovy.lang.GString.toString(GString.java:160)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:540)
at org.codehaus.groovy.runtime.InvokerHelper.toString(InvokerHelper.java:112)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.println(DefaultGroovyMethods.java:571)
at groovy.lang.Script.println(Script.java:155)
I would expect it to output 'null' in this case but it appears to be
having problems formatting the value.
Any suggestions?
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
Are you sure the data structure you're describing is what's _actually_
being returned? I tell everyone this, but turn on 'wire' logging for
Apache HttpClient as described
here:http://groovy.codehaus.org/modules/http-builder/doc/index.html#Logging_and_Debugging
And verify that 'results' isn't null. That's what I would expect to
cause this error. If I have a chance, I'll try to do a quick test of
my own.
-Tom
Roshan, your solution works perfectly, thanks. I'll have to
investigate to find out why :)
Thank you both,
Richard.
2009/8/22 Tom Nichols <tmni...@gmail.com>: