Android的 - 解析使用GSON JSON [英] Android - Parse JSON using GSON

查看:117
本文介绍了Android的 - 解析使用GSON JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JSON是字符串类型的分析数据。我使用谷歌GSON。我不知道我怎样才能得到OriginalTerm,这JSON字符串的FirstTranslation的信息:

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 /

JSON解析

JSON parsing using Gson for java

我试图用GSON使用POJO来序列化,但我无法找到合适的结构,我尝试使用的JSONObject过,通过对象键跳跃,如term0,PrincipalTranslations但我有当我多个结果有些麻烦对于相同的键,例如:

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"...
    }

感谢您提前。

推荐答案

解析可以使用映射一比一的JSON文本与你的对象POJO来完成与GSON一个JSON。但是,因为你需要的JSON字符串的一部分,你可以利用 JsonParser 对象,它可以让你得到的只是你的JSON的部分。

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.

所以,你可以得到 PrincipalTranslation 部分,然后应用POJO策略牢记,你至少有两种结构:你的期限和两期限和记组成(我称之为项目

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).

请记住,POJO我写的是不遵守的Java命名惯例,这样你就可以添加的标注使用不同的成员变量名。

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.

下面是c您一个$ C $可以粘贴在你的IDE中运行试试。

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

这篇关于Android的 - 解析使用GSON JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆