简体   繁体   中英

How to write the necessary java class to use gson.fromJson() for the following json object?

I am trying to parse the following JSON object:

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

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

I tried the following:

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

    public class KEY3{



        public class SUBKEY{
            String subsubkey1;
            String subsubkey2;

            //getters and setters
        }
    }

    //getters and setters

}

and then did the following:

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

where jsonMessage is the JSON string above and subkeys are dynamic so i do not know how many of them are there.. But key3 becomes null . So, my problem is, how can I get key3 and its subkeys and subvalues using gson.fromJson ? I do not want to do it like the following:

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

I want to use gson.fromJson(); directly.

您在KEY3中需要两个实例变量,分别为SUBKEY类型的subkey1和subkey2。

It should work fine. Provided the key3 class should have fields subKey1 & subKey2. Eg

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);
    }

}

You can try with three different POJO classes that is exact replica of this JSON string.

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

output:

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

If keys are dynamic and JSON string is not known in advance then try with Map<String,Object> using TypeToken

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

You're just confused with nested classes and inner classes. The inner class (without static in definition) is some class, which instances always have "parent" instance. As for nested classes (with static ), they're independent on the "parent" class and that's what we need in the case. So, the solution would be so:

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

}

For the better clarification, you should look at this thread: http://code.google.com/p/google-gson/issues/detail?id=135

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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