如何编写必要的java类来使用gson.fromJson()来获取以下json对象?

时间:2014-08-07 12:49:56

标签: java json gson

我正在尝试解析以下JSON对象:

{
    "key1": "value1",
    "key2": "value2",
    "key3": {
          "subkey1": {
                "subsubkey1": "value",
                "subsubkey2": "value"
           },
           "subkey2": {
                "subsubkey1": "value",
                "subsubkey2": "value"
           }

          ...........other dynamic subkeys..............
    }
}

我尝试了以下内容:

public class MyObject{
    String key1, key2;
    KEY3 key3;

    public class KEY3{



        public class SUBKEY{
            String subsubkey1;
            String subsubkey2;

            //getters and setters
        }
    }

    //getters and setters

}

然后执行以下操作:

MyObject mObject = gson.fromJson(jsonMessage, MyObject.class);

其中jsonMessage是上面的JSON字符串,子键是动态的,所以我不知道它们中有多少...但key3变为null。所以,我的问题是,如何使用key3获取gson.fromJson及其子键和子值?我不想这样做:

JSONObject jObject= new JSONObject(jsonMessage);
JSONObject key3Object = jObject.getJsonObject("key3");

我想直接使用gson.fromJson();

4 个答案:

答案 0 :(得分:1)

在KEY3中需要两个名为subkey1的实例变量和一个SUBKEY类型的子键。

答案 1 :(得分:1)

应该可以正常工作。如果key3类应该有字段subKey1& subKey2。例如

import java.util.Map;

import com.google.gson.Gson;

class MyObject {
    String key1, key2;
    Map<String, SUBKEY> key3;

    public class SUBKEY {
        String subsubkey1;
        String subsubkey2;

        public String getSubsubkey1() {
            return subsubkey1;
        }

        public void setSubsubkey1(String subsubkey1) {
            this.subsubkey1 = subsubkey1;
        }

        public String getSubsubkey2() {
            return subsubkey2;
        }

        public void setSubsubkey2(String subsubkey2) {
            this.subsubkey2 = subsubkey2;
        }

        @Override
        public String toString() {
            return "SUBKEY [subsubkey1=" + subsubkey1 + ", subsubkey2="
                    + subsubkey2 + "]";
        }
    }

    public String getKey1() {
        return key1;
    }

    public void setKey1(String key1) {
        this.key1 = key1;
    }

    public String getKey2() {
        return key2;
    }

    public void setKey2(String key2) {
        this.key2 = key2;
    }

    public Map<String, SUBKEY> getKey3() {
        return key3;
    }

    public void setKey3(Map<String, SUBKEY> key3) {
        this.key3 = key3;
    }

    @Override
    public String toString() {
        return "MyObject [key1=" + key1 + ", key2=" + key2 + ", key3=" + key3
                + "]";
    }
}

public class Sample {

    public static void main(String[] args) {
        String jsonMessage = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":{\"subkey1\":{\"subsubkey1\":\"value\",\"subsubkey2\":\"value\"},\"subkey2\":{\"subsubkey1\":\"value\",\"subsubkey2\":\"value\"}}}";
        Gson gson = new Gson();
        MyObject mObject = gson.fromJson(jsonMessage, MyObject.class);
        System.out.println(mObject);
    }

}

答案 2 :(得分:1)

您可以尝试使用三个不同的POJO类,它们是此JSON字符串的精确副本。

class MyObject {
    private String key1, key2;
    private KEY3 key3;
}

class KEY3 {
    private SUBKEY3 subkey1;
    private SUBKEY3 subkey2;
    // getters and setters
}

class SUBKEY3 {
    private String subsubkey1;
    private String subsubkey2;
}

...

MyObject data = new Gson().fromJson(jsonString, MyObject.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

输出:

{
  "key1": "value1",
  "key2": "value2",
  "key3": {
    "subkey1": {
      "subsubkey1": "value",
      "subsubkey2": "value"
    },
    "subkey2": {
      "subsubkey1": "value",
      "subsubkey2": "value"
    }
  }
}

如果密钥动态并且事先不知道JSON字符串,请使用TypeToken

尝试使用Map<String,Object>
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> data = new Gson().fromJson(jsonString, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));

答案 3 :(得分:0)

您只是与嵌套类和内部类混淆。内部类(定义中没有static)是某个类,这些实例总是具有“父”实例。对于嵌套类(使用static),它们独立于“父”类,这就是我们在案例中所需要的。所以,解决方案就是这样:

public class MyObject {
    String key1, key2;
    Key3 key3;

    public static class Key3 {

        public static class Subkey {
            String subsubkey1;
            String subsubkey2;    
            //getters and setters

        }
    }

    //getters and setters

}

为了更好地澄清,你应该看一下这个帖子:http://code.google.com/p/google-gson/issues/detail?id=135