I have been looking around for examples related to converting JSON strings to Java object but haven't found any good examples. The one I found was really basic once and not really dealing with complex JSON strings.
I am making an app to translate strings from english to different languages using google translate api. Google's response upon query is...foolowing text is formatted in JSON,
{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}}
my approach so far is using GSON API, however, I am stuck by actually how should I manipulate this complicated result and create java object?
My java class is...
import com.google.gson.Gson;
public class JSONConverter {
private String traslatedText;
/**
* Create an object of it self by manipulating json string
* @param json type: String
* @return String Translated text result from JSON responce
*/
public String getTranslation(String json){
Gson gson = new Gson();
JSONConverter obj = gson.fromJson(json, JSONConverter.class);
return obj.getTranslationForReturn();
}
/**
* Method return a translation to a private call
* @return String translation
*/
private String getTranslationForReturn(){
return this.traslatedText;
}
}
Above approach is not working since I am not getting "Bonjour tout le monde" on return,
it would be a great pleasure if someone can extend my understanding.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…