2

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.

5
  • 4
    What did you try? What are you having trouble with? Show us your code. Commented Dec 2, 2013 at 20:58
  • Do you need all data or only a portion of it? Commented Dec 2, 2013 at 21:10
  • @SLaks I tried to serialized JSON Objects using POJO. I tried too using JsonObject but I have some trouble when I've 2 keys inside "PrincipalTranslations" :( Commented Dec 2, 2013 at 21:32
  • @giampaolo I need "PrincipalTranslations" information. Commented Dec 2, 2013 at 21:35
  • Here is an example thegeekyland.blogspot.com/2015/11/… Commented Nov 30, 2015 at 0:10

2 Answers 2

3

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=]}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @giampaolo finally I add a few line more to store information in ArrayList: //List of objects to persist List<Q20337652.Term> firstTranslationList = new ArrayList<Q20337652.Term>(); List<Q20337652.Term> originalTermList = new ArrayList<Q20337652.Term>(); List<Item> list = new ArrayList<Item>(principalTranslation.values()); for (Item item : list) { firstTranslationList.add(item.FirstTranslation); originalTermList.add(item.OriginalTerm); } Thanks again :)
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)

1 Comment

Thanks. I tried something similar the problem is when I found a list of JSONObjects like "PrincipalTranslations", it has 2 keys how I parse it like JsonObject?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.