This is simple code to do it, I avoided all checks but this is the main idea.
public String parse(String jsonLine) {
JsonElement jelement = JsonParser.parseString(jsonLine);
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("data");
JsonArray jarray = jobject.getAsJsonArray("translations");
jobject = jarray.get(0).getAsJsonObject();
String result = jobject.get("translatedText").getAsString();
return result;
}
To make the use more generic - you will find that Gson's javadocs are pretty clear and helpful.
Answer from MByD on Stack OverflowThis is simple code to do it, I avoided all checks but this is the main idea.
public String parse(String jsonLine) {
JsonElement jelement = JsonParser.parseString(jsonLine);
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("data");
JsonArray jarray = jobject.getAsJsonArray("translations");
jobject = jarray.get(0).getAsJsonObject();
String result = jobject.get("translatedText").getAsString();
return result;
}
To make the use more generic - you will find that Gson's javadocs are pretty clear and helpful.
In my first gson application I avoided using additional classes to catch values mainly because I use json for config matters
despite the lack of information (even gson page), that's what I found and used:
starting from
Map jsonJavaRootObject = new Gson().fromJson("{/*whatever your mega complex object*/}", Map.class)
Each time gson sees a {}, it creates a Map (actually a gson StringMap )
Each time gson sees a '', it creates a String
Each time gson sees a number, it creates a Double
Each time gson sees a [], it creates an ArrayList
You can use this facts (combined) to your advantage
Finally this is the code that makes the thing
Map<String, Object> javaRootMapObject = new Gson().fromJson(jsonLine, Map.class);
System.out.println(
(
(Map)
(
(List)
(
(Map)
(
javaRootMapObject.get("data")
)
).get("translations")
).get(0)
).get("translatedText")
);
Based on the javadoc for Gson 2.8.6
No need to instantiate this class, use the static methods instead.
and following are the alternatives to be used.
// jsonString is of type java.lang.String
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
// reader is of type java.io.Reader
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
// jsonReader is of type com.google.gson.stream.JsonReader
JsonObject jsonObject = JsonParser.parseReader(jsonReader).getAsJsonObject();
Example
import static org.junit.Assert.assertTrue;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Test {
public static void main(String[] args) {
String jsonString = "{ \"name\":\"John\"}";
JsonObject jsonObjectAlt = JsonParser.parseString(jsonString).getAsJsonObject();
// Shows deprecated warning for new JsonParser() and parse(jsonString)
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
assertTrue(jsonObjectAlt.equals(jsonObject));
}
}
new GsonBuilder().setPrettyPrinting().create().toJson(new JSONParser(jsonString).parse());