Skip to content Skip to sidebar Skip to footer

Android - Parse Json Using Gson

I would like to parse data from JSON which is String type. I am using Google Gson. I'm wondering how can I get 'OriginalTerm' and 'FirstTranslation' information of this Json String

Solution 1:

Parsing a JSON with Gson can be done using a POJO that maps one-to-one your JSON text with your object. But, since you need only part of the JSON string, you can take advantage of JsonParser object that allows you to get a portion only of your JSON.

So you can get PrincipalTranslation part and then apply the POJO strategy keeping in mind that you have at least two structures: your Term and a composition of two Terms and a note (that I called Item).

Keep in mind that POJO I write are not following Java naming convention, so you can add an annotation to use a different member variable name.

Here's a code you can paste and run in you IDE to try.

package stackoverflow.questions;

import java.lang.reflect.Type;
import java.util.*;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

publicclassQ20337652 {

   publicstaticclassTerm {
      String term;
      StringPOS;
      String sense;
      String usage;

      @OverridepublicString toString() {
         return"Term [term="+ term +", POS="+POS+", sense="+ sense +", usage="+ usage +"]";
      }


   }

   publicstaticclassItem {
      TermOriginalTerm;
      TermFirstTranslation;
      StringNote;

      @OverridepublicString toString() {
         return"Item [OriginalTerm="+OriginalTerm+", FirstTranslation="+FirstTranslation+", Note="+Note+"]";
      }


   }



   publicstatic void main(String[] args){

          String json ="  {                                                                                                                                   "+"                                                                                                                                      "+"     \"term0\" : {                                                                                                                    "+"     \"PrincipalTranslations\" : {                                                                                                    "+"         \"0\" :{                                                                                                                     "+"             \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"domestic animal\", \"usage\" : \"\"},            "+"             \"FirstTranslation\" : {\"term\" : \"gato\", \"POS\" : \"nm\", \"sense\" : \"\"}, \"Note\" : \"\"},                    "+"         \"1\" :{                                                                                                                     "+"             \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"member of cat family\", \"usage\" : \"\"},       "+"             \"FirstTranslation\" : {\"term\" : \"felino\", \"POS\" : \"nm\", \"sense\" : \"familia de animales\"}, \"Note\" : \"\"}},"+"     \"AdditionalTranslations\" : {                                                                                                   "+"         \"0\" :{                                                                                                                     "+"             \"OriginalTerm\" : { \"term\" : \"cat\", \"POS\" : \"n\", \"sense\" : \"guy\", \"usage\" : \"slang\"},                   "+"             \"FirstTranslation\" : {\"term\" : \"tío, tipo, chaval\", \"POS\" : \"nm\", \"sense\" : \"coloq\"},                      "+"             \"SecondTranslation\" : {\"term\" : \"vato\", \"POS\" : \"\", \"sense\" : \"Mex\"}, \"Note\" : \"\"},                    "+"                                                                                                                                      "+"     \"original\" : {                                                                                                                 "+"     \"Compounds\" : {                                                                                                                "+"         \"0\" :{                                                                                                                     "+"             \"OriginalTerm\" : { \"term\" : \"alley cat\", \"POS\" : \"n\", \"sense\" : \"stray cat\", \"usage\" : \"\"},            "+"             \"FirstTranslation\" : {\"term\" : \"gato callejero\", \"POS\" : \"nm\", \"sense\" : \"\"}, \"Note\" : \"\"},            "+"     \"Lines\" : \"End Reached\", \"END\" : true                                                                                      "+"                                                                                                                                      "+" }                                                                                                                                    "+" }     }}}                                                                                                                               ";
      JsonParser jp = new JsonParser();
      JsonElement je = jp.parse(json);
      JsonElement je2 = je.getAsJsonObject().get("term0");
      JsonElement je3 = je2.getAsJsonObject().get("PrincipalTranslations");

      Type mapType = new TypeToken<Map<String, Item>>() {}.getType();

      Map<String, Item> principalTranslation = new Gson().fromJson(je3, mapType);

      System.out.println(principalTranslation);


   }

}

And this is my execution:

{0=Item [OriginalTerm=Term [term=cat, POS=n, sense=domestic animal, usage=], FirstTranslation=Term [term=gato, POS=nm, sense=  , usage=null], Note=], 
1=Item [OriginalTerm=Term [term=cat, POS=n, sense=member of cat family, usage=], FirstTranslation=Term [term=felino, POS=nm, sense=familia de animales, usage=null], Note=]}

Solution 2:

I think this could help: http://www.newthinktank.com/2013/07/android-development-15/

It's a video tutorial explaining the how to use json. Not through GSON though, but I think you need a base on how JSON works before getting involved in GSON (it's rather complicated)

Post a Comment for "Android - Parse Json Using Gson"