jQuery in Action, 3rd edition
The moose likes Web Services and the fly likes How to Parse JASON response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to Parse JASON response" Watch "How to Parse JASON response" New topic

How to Parse JASON response

Shweta Machhe

Joined: Nov 22, 2011
Posts: 8
Hi ,

I want to translate my english string to french. so i am using https://www.googleapis.com/language/translate/ API.
It works as follows

https://www.googleapis.com/language/translate/v2?key=YOUR KEY&q=TEXT TO TRANSLATE&source=SOURCE LANGUAGE CODE&target=TARGET LANGUAGE CODE

it returns me a JASON object.
"data": {
"translations": [
"translatedText": "bonjour"

How should i parse it, to access translated sting in my java code?
Tim Moores

Joined: Sep 21, 2011
Posts: 2409
On json.org you'll find numerous Java libraries for handling JSON data.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63222


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hi Shweta,

To parse JSON in Java you'll need to import json-lib-2.2.2-jdk15.jar into your project.
Once you have done that all you need to do is

where s is the string representing the returned JSON string.
This will give you an Object. Convert it into JSONArray

and get the element you need, for instance

Hope this helps.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
An alternative may be Jackson, a JSON parser which performs well and is easy to work with: http://jackson.codehaus.org/
Best wishes!

My free books and tutorials: http://www.slideshare.net/krizsan
It is sorta covered in the JavaRanch Style Guide.
subject: How to Parse JASON response
It's not a secret anymore!