JSON parsing in Java using Gson

0 votes

I have the following code which I am trying to parse from JSON using Google GSON: 

jsonLine = "
{
 "data": {
  "translations": [
   {
    "translatedText": "Hello world"
   }
  ]
 }
}
";

JsonParsing class

public class JsonParsing{
   public void parse(String jsonLine) {
      // ---------Get Hello World Here-------
   }
}

How do I fetch the "Hello World" string in my class? Please help.

Nov 22, 2018 in Java by 93.lynn
• 1,600 points
1,998 views

1 answer to this question.

0 votes

Well, you can easily do that by avoiding the checks. Follow the below code:

 public String parse(String jsonLine) {
    JsonElement jsonEle = new JsonParser().parse(jsonLine);
    JsonObject  jsonObj = jsonEle.getAsJsonObject();
    jsonObj = jsonObj.getAsJsonObject("data");
    JsonArray jsonArr = jsonObj.getAsJsonArray("translations");
    jsonObj = jsonArr.get(0).getAsJsonObject();
    String res = jsonObj.get("translatedText").getAsString();
    return res;
}

For more info, you can refer here: Gson's javadocs.

answered Nov 22, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,480 points
3,829 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,923 views
0 votes
3 answers

How to parse JSON in Java

import org.json.*; JSONObject obj = new JSONObject(" .... ...READ MORE

answered Aug 20, 2018 in Java by Daisy
• 8,140 points
4,015 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,619 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,260 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,190 points
6,995 views
0 votes
1 answer

How can we return a JSON object from a Java Servlet?

response.setContentType("application/json"); // Get the printwriter object from response ...READ MORE

answered Jul 6, 2018 in Java by sharth
• 3,370 points
5,890 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,500 points
4,703 views
0 votes
1 answer

Need for finalize() in Java

finalize() is a method called by the ...READ MORE

answered May 9, 2018 in Java by code.reaper12
• 3,500 points
687 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP