I use the REST api of google to trigger a search on their search engine. This works as expected. The result of the call is a complex JSON string.

See http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=ivyTeam sampleJson

How can I comfortably access this complex result in ivy. With Java or ivyScript?

asked 08.09.2015 at 16:23

SupportIvyTeam's gravatar image

SupportIvyTeam ♦♦
accept rate: 77%

edited 08.09.2015 at 16:53

You can easily access the JSON response content if you:

  • use a java pojo generator that creates a complex java class structure out of a JSON string
  • use Gson to parse the JSON string into instances of the generated java classes.

Generate classes:

  • Copy the whole JSON string into your clipboard and paste it into a java generator: http://www.jsonschema2pojo.org/
  • Configure the generator:
  • package = com.google.generated
  • class = QueryResponse
  • source type = JSON
  • annotation style = GSON
  • generate the java source by pressing on the "JAR" button.
  • download the generated sources jar.
  • copy the content of the sources-jar into your ivy projects sources folder

Parse JSON as java objects

  • add the latest gson.jar to our classpath: download the jar -> switch to java perspective -> copy the jar into a directory of the project -> right click on jar -> build path -> add to build path
  • QueryResponse response = new Gson().fromJson("{yourComplexResponse}", QueryResponse.class);

java pojo generator json pojos in ivy project convert json to java


answered 08.09.2015 at 16:27

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
accept rate: 70%

edited 08.09.2015 at 16:49

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 08.09.2015 at 16:23

Seen: 4,768 times

Last updated: 08.09.2015 at 16:53