简体   繁体   English

使用gson将JSON转换为Java对象

[英]JSON to java object using gson

I am using GSON library for turing JSON that comes form a web service but I can't make it work, I always get a null. 我正在使用GSON库来浏览来自Web服务的JSON,但是我无法使其工作,我总是得到null。 I've looked through similar issues like Converting Json to Java such as Simple Json to Java convertion using GSON . 我研究过类似的问题,例如使用GSON将Json转换为Java,例如从简单Json转换为Java But I am still missing something 但是我仍然缺少一些东西

JSON JSON格式

{"A":"val1","B":"val2","C":"val3","D":"val4","E":"val5","F":"val6","G":"val7"}
         SiteWrapper m = gson.fromJson(json, SiteWrapper.class);

java Class java类

SiteWrapper m = gson.fromJson(json, SiteWrapper.class);
System.out.println(m.getMenu());

static class Site {
    static String A;
    static String B;
    static String C;
    static String D;
    static String E;
    static String F;
    static String G;

    public String toString() {
        return String.format(A,B,C,D,E,F,G);}

    public static String getA() {
        return A;
    }
    public static String getB() {
        return B;
    } 
... all the way to getG

    public void setA(String A) {
        Site.A = A;
    }
    public void setB(String B) {
        Site.B = B;
    }
... all the way to setB

and my wrapper 还有我的包装纸

class SiteWrapper {
    private Site site;
    public Site getMenu() { return site; }
    public void setMenu(Site site) { this.site = site; }
}

no matter what I do I get a null printed , any ideas? 无论我怎么打印空值,有什么想法吗?

Since its a static inner class .As docs pointed out and comments : 由于它是一个静态内部类。如文档所指出和注释:

As well, if a field is marked as "static" then by default it will be excluded. 同样,如果字段标记为“静态”,则默认情况下将其排除。 If you want to include some transient fields... 如果要包括一些瞬态字段...

You may want to try 您可能要尝试

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

Also, since its a inner class you may need to change your JSON If you can: 此外,由于它是内部类,因此您可能需要更改JSON。如果可以:

 {
   "site":{
      "A":"val1",
      "B":"val2",
      "C":"val3",
      "D":"val4",
      "E":"val5",
      "F":"val6",
      "G":"val7"
   }
}

As noted here in this post 如这篇文章中所述

The issue is that in your code you're passing SiteWrapper.class when you should be passing Site.class to gson.fromJSON 问题是,当您应该将Site.class传递给SiteWrapper.class时,您正在传递代码中的gson.fromJSON

This line 这条线

SiteWrapper m = gson.fromJson(json, SiteWrapper.class);

should be 应该

Site s = gson.fromJSON(json, Site.class);

Site is the class you defined for the JSON provided. Site是您为提供的JSON定义的类。 SiteWrapper contains a site variable, you need to set this Site variable to the result of the fromJSON SiteWrapper包含一个网站变量,您需要将此Site变量设置为fromJSON的结果

Per this documentation, all static fields are excluded by default. 根据文档,默认情况下不包括所有静态字段。 Follow the example in the link to alter the default exclusion strategy so that statics are accepted. 按照链接中的示例更改默认排除策略,以便接受静态数据。

When you create your Gson object, try the following: 创建Gson对象时,请尝试以下操作:

Gson gson = new GsonBuilder()
    .excludeFieldsWithModifier(Modifier.TRANSIENT,Modifier.VOLATILE)
    .create();

This should create a Gson object that will not exclude static fields by default. 这将创建一个Gson对象,该对象默认情况下不会排除静态字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM