使用 GSON 将 json 解析为 java,其中 json 在另一个对象中有动态对象
parsing json to java using GSON where json have dynamic objects inside another object
我这里有一些奇怪的问题。
我有样品JSON。
"properties": {
"emailID": {
"type": "string"
},
"createdDate": {
"type": "date"
},
"lastModifiedDate": {
"type": "date"
}
}
我这里用的是GSON解析。而且我知道我将不得不编写 Properties Class 来解析它。
但是在 properties 元素内部,所有元素(emailId
、createdDate
、lastModifiedDate
)都是动态的,另一个 JSON
在 properties 元素内部可能有 4 个对象,例如"phoneNumber"。
在此处需要帮助来编写属性 class,以便在属性元素中包含多少属性,这些是如何在属性对象中转换为对象的。
如有任何帮助或提示,我们将不胜感激。
提前致谢!
你可以这样做。您可以使用 Map,因为您的键和值都是动态的。
public class Wrapper {
public Map<String, Map<String, String>> properties;
}
您甚至可以去掉包装器 class 并直接使用 Map<String, Map<String, String>>
我这里有一些奇怪的问题。
我有样品JSON。
"properties": {
"emailID": {
"type": "string"
},
"createdDate": {
"type": "date"
},
"lastModifiedDate": {
"type": "date"
}
}
我这里用的是GSON解析。而且我知道我将不得不编写 Properties Class 来解析它。
但是在 properties 元素内部,所有元素(emailId
、createdDate
、lastModifiedDate
)都是动态的,另一个 JSON
在 properties 元素内部可能有 4 个对象,例如"phoneNumber"。
在此处需要帮助来编写属性 class,以便在属性元素中包含多少属性,这些是如何在属性对象中转换为对象的。
如有任何帮助或提示,我们将不胜感激。 提前致谢!
你可以这样做。您可以使用 Map,因为您的键和值都是动态的。
public class Wrapper {
public Map<String, Map<String, String>> properties;
}
您甚至可以去掉包装器 class 并直接使用 Map<String, Map<String, String>>