使用具有多个对象值的 Gson

发布于 2025-01-07 06:37:58 字数 1545 浏览 1 评论 0原文

我有这个 Json 代码:

{
 "term" : {
   "PrincipalTranslations" : {
      "0" : {
            termine:"casa", 
            traduzione:"home"
            }
      "1" :{
             termine:"testa",
             traduzione:"head"
           }
       "2" :{
             termine:"dito",
             traduzione:"finger"
           }
   }
 }
}

How can I deserialize the object 0, 1, 2?? 如果我写对象“零”(并停止)而不是对象 0、1、2,它就可以工作! 我已经使用了这个实现:

public class Item {    
        private term term;

        public term getTERM() {
                return term;
        }  
}

public class term {
       private PrincipalTranslations PrincipalTranslations;

        public PrincipalTranslations getPrincipalTranslations() {
                return PrincipalTranslations;
        }      
}

public class PrincipalTranslations {
        private zero zero;

        public zero getZero() {
                return zero;
        }
}

public class zero {
        private String termine;

        public String gettermine() {
                return termine;
        }
}

并使用它,它打印(以正确的方式)“casa”

public class MainClass {

    public static void main(String[] args) throws IOException {

        FileReader reader = new FileReader("/home/peppe/test_ff"); 

        Gson gson = new GsonBuilder().create();

        Item p = gson.fromJson(reader, Item.class);
        System.out.print(p.getTERM().getPrincipalTranslations().getZero().gettermine());


        reader.close();
      }
}

I have this Json code:

{
 "term" : {
   "PrincipalTranslations" : {
      "0" : {
            termine:"casa", 
            traduzione:"home"
            }
      "1" :{
             termine:"testa",
             traduzione:"head"
           }
       "2" :{
             termine:"dito",
             traduzione:"finger"
           }
   }
 }
}

How can I deserialize the object 0, 1, 2??
If instead of object 0, 1, 2 I wrote object "zero" (and stop), it works!
I've used this implementation:

public class Item {    
        private term term;

        public term getTERM() {
                return term;
        }  
}

public class term {
       private PrincipalTranslations PrincipalTranslations;

        public PrincipalTranslations getPrincipalTranslations() {
                return PrincipalTranslations;
        }      
}

public class PrincipalTranslations {
        private zero zero;

        public zero getZero() {
                return zero;
        }
}

public class zero {
        private String termine;

        public String gettermine() {
                return termine;
        }
}

and use it so, it print (in the right way) "casa"

public class MainClass {

    public static void main(String[] args) throws IOException {

        FileReader reader = new FileReader("/home/peppe/test_ff"); 

        Gson gson = new GsonBuilder().create();

        Item p = gson.fromJson(reader, Item.class);
        System.out.print(p.getTERM().getPrincipalTranslations().getZero().gettermine());


        reader.close();
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2025-01-14 06:37:58

如果您想将对象调用为零,请在“Principal Translations”类中使用“SerializedName”注释:http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html

它看起来像这样:

@SerializedName("0")
public Zero zero;

If you want to call the object zero, than in your ’Principal Translations‘ class use the ’SerializedName’ annotation: http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html

It will look like this:

@SerializedName("0")
public Zero zero;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文