I am trying to convert a JSON Object to JAVA Bean using JSON lib.
The JSON format is:
{
"services": [
{
"url": "https://stashboardni.appspot.com/api/v1/services/fpga-compile-cloud…",
"id": "fpga-compile-cloud-beta",
"description": "FPGA Compile Cloud Beta",
"name": "FPGA Compile Cloud Beta",
"current-event": {
"status": {
"description": "The service is up",
"level": "NORMAL",
"url": "https://stashboardni.appspot.com/api/v1/statuses/up",
"image": "https://stashboardni.appspot.com/images/status/tick-circle.png",
"id": "up",
"name": "up"
},
"url": "https://stashboardni.appspot.com/api/v1/services/fpga-compile-cloud…",
"timestamp": "Mon, 14 Feb 2011 22:35:38 GMT",
"sid": "agxzdGFzaGJvYXJkbmlyDQsSBUV2ZW50GIH6AQw",
"message": "Service is working properly.",
"informational": false
}
},
{
"url": "https://stashboardni.appspot.com/api/v1/services/labview-web-ui-bui…",
"id": "labview-web-ui-builder",
"description": "LabVIEW Web UI Builder",
"name": "LabVIEW Web UI Builder",
"current-event": {
"status": {
"description": "The service is up",
"level": "NORMAL",
"url": "https://stashboardni.appspot.com/api/v1/statuses/up",
"image": "https://stashboardni.appspot.com/images/status/tick-circle.png",
"id": "up",
"name": "up"
},
"url": "https://stashboardni.appspot.com/api/v1/services/labview-web-ui-bui…",
"timestamp": "Thu, 17 Feb 2011 15:50:50 GMT",
"sid": "agxzdGFzaGJvYXJkbmlyDQsSBUV2ZW50GNKJAgw",
"message": "Service is working properly",
"informational": false
}
},
{
"url": "https://stashboardni.appspot.com/api/v1/services/ni-hosted-services…",
"id": "ni-hosted-services-emails",
"description": "The system that handles sending emails for password resets, new acc…",
"name": "NI Hosted Services Emails",
"current-event": {
"status": {
"description": "The service is up",
"level": "NORMAL",
"url": "https://stashboardni.appspot.com/api/v1/statuses/up",
"image": "https://stashboardni.appspot.com/images/status/tick-circle.png",
"id": "up",
"name": "up"
},
"url": "https://stashboardni.appspot.com/api/v1/services/ni-hosted-services…",
"timestamp": "Mon, 14 Feb 2011 22:36:03 GMT",
"sid": "agxzdGFzaGJvYXJkbmlyDQsSBUV2ZW50GJXLAQw",
"message": "Service is working properly.",
"informational": false
}
},
{
"url": "https://stashboardni.appspot.com/api/v1/services/ni-hosted-services…",
"id": "ni-hosted-services-login",
"description": "NI Hosted Services Login",
"name": "NI Hosted Services Login",
"current-event": {
"status": {
"description": "The service is up",
"level": "NORMAL",
"url": "https://stashboardni.appspot.com/api/v1/statuses/up",
"image": "https://stashboardni.appspot.com/images/status/tick-circle.png",
"id": "up",
"name": "up"
},
"url": "https://stashboardni.appspot.com/api/v1/services/ni-hosted-services…",
"timestamp": "Mon, 14 Feb 2011 22:36:55 GMT",
"sid": "agxzdGFzaGJvYXJkbmlyDQsSBUV2ZW50GJbLAQw",
"message": "Service is working properly. ",
"informational": false
}
}
]
}
My Java code which serializes this JSON to Java Bean is:
jsonResp = JSONObject.fromObject(strResponse);
if (jsonResp != null) {
sList = MorphToJavaBean(jsonResp);
}
private StashBoardServices MorphToJavaBean(JSONObject prmJSONObject) {
if (prmJSONObject == null) {
return null;
}
StashBoardServices sServices = null;
Map classMap = null;
MorpherRegistry morpherRegistry = null;
Morpher dynaMorpher = null;
List services = null;
try {
JsonConfig jsonConfig = new JsonConfig();
classMap = new HashMap();
classMap.put("services", StashBoardService.class);
jsonConfig.setRootClass(StashBoardServices.class);
jsonConfig.setClassMap(classMap);
sServices = new StashBoardServices();
sServices = (StashBoardServices) JSONObject.toBean(prmJSONObject, jsonConfig);
morpherRegistry = JSONUtils.getMorpherRegistry();
dynaMorpher = new BeanMorpher(StashBoardService.class, morpherRegistry);
morpherRegistry.registerMorpher(dynaMorpher);
services = new ArrayList();
for (Iterator iter = sServices.getServices().iterator(); iter.hasNext();) {
services.add(morpherRegistry.morph(StashBoardService.class, iter.next()));
}
sServices.setServices(services);
} catch (Exception ex) {
Logger.getLogger(StashBoardController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
return sServices;
}
}
I have created Beans as follows:
Services:
public class StashBoardServices
{
private List services;
public StashBoardServices()
{
}
public List getServices() {
return services;
}
public void setServices(List services) {
this.services = services;
}
}
Service:
package com.StashBoard;
/**
*
-
@author gkhadke
*/
public class StashBoardService
{
private String url;
private String id;
private String description;
private String name;
private StashBoardCurrentEvent currentevent;
public StashBoardService()
{
}
public StashBoardCurrentEvent getCurrentevent() {
return currentevent;
}
public void setCurrentevent(StashBoardCurrentEvent currentevent) {
this.currentevent = currentevent;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
and other JAVA bean classes are also defined.
I am getting error since there is "current-event" KV pair in JSONObject response which I can not map to my "currentevent" field in Java Bean.
I wanted to know if there is any annotation support which can resolve this in Json lib?