Reputation: 10777
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.
Upvotes: 0
Views: 707
Reputation: 7461
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
Upvotes: 0
Reputation: 8499
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);
}
}
Upvotes: 1
Reputation: 46841
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));
Upvotes: 1
Reputation: 2573
You need two instance variables in KEY3 called subkey1 and subkey2 of type SUBKEY.
Upvotes: 1