galix85
galix85

Reputation: 167

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:

    {

        "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

    }     

I tried following these information but I can't solve it:

http://albertattard.blogspot.com.es/2009/06/practical-example-of-gson.html

JSON parsing using Gson for Java

I tried to serialized using GSON using POJO but I can't find the right structure, I tried using JsonObject too, jumping through object keys like "term0","PrincipalTranslations" but I have some trouble when I've multiple results for the same key, for example:

    "0" :{
            "OriginalTerm".... 
            "FirstTranslation"...
        "1" :{
            "OriginalTerm".... 
            "FirstTranslation"...
    }

Thank you for advance.

Upvotes: 2

Views: 9403

Answers (2)

giampaolo
giampaolo

Reputation: 6934

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;

public class Q20337652 {

   public static class Term {
      String term;
      String POS;
      String sense;
      String usage;

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


   }

   public static class Item {
      Term OriginalTerm;
      Term FirstTranslation;
      String Note;

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


   }



   public static 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=]}

Upvotes: 3

Unu
Unu

Reputation: 771

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)

Upvotes: 2

Related Questions