Martynas Martynas - 8 months ago 41
Java Question

JSON parsing using Gson for java

I would like to parse data from JSON which is String type.
I am using Google Gson.

I have

jsonLine = "
"data": {
"translations": [
"translatedText": "Hello world"

and my class is:

public class JsonParsing{

public void parse(String jsonLine) {

// there I would like to get String "Hello world"




This is simple code to do it, I avoided all checks but this is the main idea.

 public String parse(String jsonLine) {
    JsonElement jelement = new JsonParser().parse(jsonLine);
    JsonObject  jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("data");
    JsonArray jarray = jobject.getAsJsonArray("translations");
    jobject = jarray.get(0).getAsJsonObject();
    String result = jobject.get("translatedText").toString();
    return result;

To make the use more generic - you will find that Gson's javadocs are pretty clear and helpful.