*
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
Author

How to Parse JASON response

Shweta Machhe
Greenhorn

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
Rancher

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

Joined: Jan 10, 2002
Posts: 60782
    
  65

JSON, not JASON,


[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
    
    1
Hi!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Parse JASON response
 
Similar Threads
login and redirection
web service security issue
Obtaining a JRadioButtons' ButtonGroup
Translation
anyone could give me comments on my class design ?