1

Hey i have this talk to do:

  • Use solr api, use this url: http://localhost:8983/solr/select/?q=tree
  • Get the answer via json/xml, deserialize it to a map objects

What is the best way to do that? I would like some hints on this. Thanks in advanced, RR

Ps. i searched for it in here, but i found examples too complex to understand.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
recoInrelax
  • 697
  • 2
  • 16
  • 33

1 Answers1

7

Does:

def json = new JsonSlurper().parseText( new URL( 'http://localhost:8983/solr/select/?q=tree' ).text )

Get you to where you want to be?

Right, so this is Grails prior to 2.0, so can't use JsonSlurper as that needs Groovy 1.8+

Can you try:

def json = grails.converters.JSON.parse( new URL( 'http://localhost:8983/solr/select/?q=tree' ).text )
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • @recoInrelax any luck? Let me know of any problems :-) – tim_yates Nov 30 '11 at 11:24
  • this import: import groovy.json.JsonSlurper nit working idk why – recoInrelax Nov 30 '11 at 11:38
  • @recoInrelax which version of Groovy? [`JsonSlurper`](http://groovy.codehaus.org/gapi/groovy/json/JsonSlurper.html) has only been in since Groovy 1.8 – tim_yates Nov 30 '11 at 11:45
  • @recoInrelax Download it [from here](http://groovy.codehaus.org/Download), and alter your path to point to it? I have a symlink (OS X/Linux) on my machine, so I just have `~/Applications/groovy` in my path, and can re-point it to whichever version I need to test – tim_yates Nov 30 '11 at 11:52
  • @recoInrelax Same error? You might have both on your path, and be still using 1.7.5... Try `println GroovySystem.version` and see what it says? – tim_yates Nov 30 '11 at 12:20
  • prints 1.7.5 . but if i type in cmd(windows): 'groovy -v' it says 1.8.4 – recoInrelax Nov 30 '11 at 12:24
  • Are you running the scripts in an IDE then? Maybe that needs updating? – tim_yates Nov 30 '11 at 12:29
  • yes. im doing a web-app in graisl, and using groovy in the controllers. idk if this have anything to do, but in libraries it says: groovy 1.6.4 – recoInrelax Nov 30 '11 at 12:30
  • @recoInrelax Oh right... You managed to avoid mentioning this was inside grails in the whole of your question and discussion... Inside Grails, you're stuck with whatever version that version of Grails is using... Grails 1.3.7 uses Groovy 1.7.8, Grails 2.0 uses Groovy 1.8+. Nothing you can do about that... Updated my answer with a possible solution (now we have found out this information) – tim_yates Nov 30 '11 at 12:37
  • No such property: grails for class: xxx.abcController – recoInrelax Nov 30 '11 at 13:04
  • @recoInrelax with `def json = grails.converters.JSON.parse( new URL( 'http://localhost:8983/solr/select/?q=tree' ).text )`? I missed the `JSON` bit with my initial update, but added it in 24 mins ago – tim_yates Nov 30 '11 at 13:05
  • ok. so json is a map. how can i get single attributes? would it be json[1].attributename?= – recoInrelax Nov 30 '11 at 13:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5459/discussion-between-recoinrelax-and-tim-yates) – recoInrelax Nov 30 '11 at 13:20
  • @recoInrelax without example json or an actual description of what you want to do, this is not going to be easy... I cannot see what you have, I don't know what you want, and you haven't said what you have tried, or why that doesn't work... – tim_yates Nov 30 '11 at 13:21
  • ok. so about this question, this is actually working which i'll accept the asnwer. join me in chat plz – recoInrelax Nov 30 '11 at 13:26
  • How will it work when HTTP response is not plain ASCII ? new URL(...).text uses default system encoding, but not specified in the request – Timur Yusupov Feb 07 '12 at 14:04
  • @TimurYusupov I guess if you're default system encoding is not set to handle the encoding being returned, you'll have to go the [long way round](http://stackoverflow.com/a/4555254/6509) as you would with Java (but that code can be groovified as well, to be shorter and more clear) – tim_yates Feb 07 '12 at 14:26
  • @tim_yates, actually the example you given uses hardcoded UTF-8 charset to decode the reponse. If there is need to use fixed UTF-8 charset, you can just use new URL( 'http://localhost:8983/solr/select/?q=tree' ).text("UTF-8") But neither example for your link nor .text("UTF-8") does not take into account charset specified in response content-type heading, which could vary and depends on server side implementation. To handle this it is needed either implement parsing content-type heading value to get charset from it or use some library like http://groovy.codehaus.org/HTTP+Builder. – Timur Yusupov Feb 08 '12 at 21:57