Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting json string to java object?

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.

like image 779
TeaCupApp Avatar asked May 21 '11 03:05

TeaCupApp


People also ask

How do you convert JSON to Java object?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

Can we convert string to object in Java?

We can also convert the string to an object using the Class. forName() method. Parameter: This method accepts the parameter className which is the Class for which its instance is required.


2 Answers

EDIT: You need your java Data class to be an exact model of the JSON. So your

{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}}  

becomes:

class DataWrapper {     public Data data;      public static DataWrapper fromJson(String s) {         return new Gson().fromJson(s, DataWrapper.class);     }     public String toString() {         return new Gson().toJson(this);     } } class Data {     public List<Translation> translations; } class Translation {      public String translatedText; } 

As the object model gets more complicated the org.json style code veers towards unreadable whereas the gson/jackson style object mapping is still just plain java objects.

like image 159
Kevin Avatar answered Oct 06 '22 00:10

Kevin


check this out Converting JSON to Java , i guess you can define a class as the object and pass that along. So, create some class and pass it along.

Data obj = gson.fromJson(json, Data.class);

class Data{   public List<Data> getTranslation(){      return translations;   } } 

Try out something like that. You may need to experiment a little.

like image 21
Matt Avatar answered Oct 05 '22 23:10

Matt