Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Parse JASON response

 
Shweta Machhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2687
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On json.org you'll find numerous Java libraries for handling JSON data.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64632
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSON, not JASON,
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
An alternative may be Jackson, a JSON parser which performs well and is easy to work with: http://jackson.codehaus.org/
Best wishes!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic