Reputation: 2255
I am trying to parse the data into a class.
But the problem is that one of the values is in Json format.
Hence I am getting a MalformedJsonException.
This is the Json string:
{
"name": "Check on Server Health ",
"state": "ABORTED",
"startTime": 1332962596131,
"triggeredBy": "GUI_MANUAL",
"completionPct": 25,
"currentStep": "sayHello",
"processDefId": "SW21SW",
"jobRetries": 0,
"businessKey": -1,
"comments": "couldn't instantiate class com.mikewidgets.helloWorld.HelloWorldTask",
"endTime": null,
"status": "DELAY , ERR",
"mtId": "MOTOROLA",
"instParms": "{"message":"start-workflow","sender":"CLI_APP","receiver":"BPM_ENG","parameters":{"workflowId":"SW21SW","mikesname":"Mikeeeeeeey","wf_HostName":"localhost","triggeredBy":"GUI_MANUAL","replyQueue":"temp-queue: //ID: SW-Demo01-51605-1332362748085-0: 2246: 1"},"userId":"Ab","mtId":"MOTOROLA","messageType":"MESSAGE_MSG"}",
"execId": "292",
"startUserId": "Ab"
}
This is the bean class I am using:
import com.google.gson.JsonElement;
public class WfActive {
private String name;
private String state;
private String startTime;
private String status;
private String endTime;
private String comments;
private String triggeredBy;
private String execId;
private JsonElement instParms;
private String startUserId;
private String mtId;
private String businessKey;
private String completionPct;
private String jobRetries;
private String processDefId;
private String currentStep;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getTriggeredBy() {
return triggeredBy;
}
public void setTriggeredBy(String triggeredBy) {
this.triggeredBy = triggeredBy;
}
public String getExecId() {
return execId;
}
public void setExecId(String execId) {
this.execId = execId;
}
public JsonElement getInstParms() {
return instParms;
}
public void setInstParms(JsonElement instParms) {
this.instParms = instParms;
}
public String getStartUserId() {
return startUserId;
}
public void setStartUserId(String startUserId) {
this.startUserId = startUserId;
}
public String getMtId() {
return mtId;
}
public void setMtId(String mtId) {
this.mtId = mtId;
}
public String getBusinessKey() {
return businessKey;
}
public void setBusinessKey(String businessKey) {
this.businessKey = businessKey;
}
public String getCompletionPct() {
return completionPct;
}
public void setCompletionPct(String completionPct) {
this.completionPct = completionPct;
}
public String getJobRetries() {
return jobRetries;
}
public void setJobRetries(String jobRetries) {
this.jobRetries = jobRetries;
}
public String getProcessDefId() {
return processDefId;
}
public void setProcessDefId(String processDefId) {
this.processDefId = processDefId;
}
public String getCurrentStep() {
return currentStep;
}
public void setCurrentStep(String currentStep) {
this.currentStep = currentStep;
}
}
But I am getting a Malformed exception at :
LA","instParms":"{"message":"start-workf // this is inside instParms
What is my mistake and how do I correct it?
Please forgive the big code, the interesting part is "instParms"
Upvotes: 0
Views: 1743
Reputation: 27233
Your JSON data is malformed (middle line below):
"mtId": "MOTOROLA",
"instParms": "{"message":"start-workflow","sender":"CLI_APP","receiver":"BPM_ENG","parameters":{"workflowId":"SW21SW","mikesname":"Mikeeeeeeey","wf_HostName":"localhost","triggeredBy":"GUI_MANUAL","replyQueue":"temp-queue: //ID: SW-Demo01-51605-1332362748085-0: 2246: 1"},"userId":"Ab","mtId":"MOTOROLA","messageType":"MESSAGE_MSG"}",
"execId": "292",
Second double quote in the value of instParms
should be escaped. Alternatively, one could use single quotes instead if you know that single quotes aren't used in the value.
This is valid:
"mtId": "MOTOROLA",
"instParms": '{"message":"start-workflow","sender":"CLI_APP","receiver":"BPM_ENG","parameters":{"workflowId":"SW21SW","mikesname":"Mikeeeeeeey","wf_HostName":"localhost","triggeredBy":"GUI_MANUAL","replyQueue":"temp-queue: //ID: SW-Demo01-51605-1332362748085-0: 2246: 1"},"userId":"Ab","mtId":"MOTOROLA","messageType":"MESSAGE_MSG"}',
"execId": "292",
and so is this:
"mtId": "MOTOROLA",
"instParms": "{\"message\":\"start-workflow\",\"sender\":\"CLI_APP\",\"receiver\":\"BPM_ENG\",\"parameters\":{\"workflowId\":\"SW21SW\",\"mikesname\":\"Mikeeeeeeey\",\"wf_HostName\":\"localhost\",\"triggeredBy\":\"GUI_MANUAL\",\"replyQueue\":\"temp-queue: //ID: SW-Demo01-51605-1332362748085-0: 2246: 1\"},\"userId\":\"Ab\",\"mtId\":\"MOTOROLA\",\"messageType\":\"MESSAGE_MSG\"}",
"execId": "292",
Another alternative would be to embed the value of instParms
as a subobject rather than a stirng:
"mtId": "MOTOROLA",
"instParms": {
"message": "start-workflow",
"sender": "CLI_APP",
"receiver": "BPM_ENG",
"parameters": {
"workflowId": "SW21SW",
"mikesname": "Mikeeeeeeey",
"wf_HostName":"localhost",
"triggeredBy":"GUI_MANUAL",
"replyQueue":"temp-queue: //ID: SW-Demo01-51605-1332362748085-0: 2246: 1"
},
"userId": "Ab",
"mtId": "MOTOROLA",
"messageType": "MESSAGE_MSG"
},
"execId": "292",
This is arguably the best solution (unless there is some other compelling reason to keep the string representation).
Upvotes: 2
Reputation: 1074276
You can't readily fix it in the code consuming the JSON, nor should you. You need to fix the other end, which is generating invalid JSON. It looks like just an erroneous quote prior to the {
on the value for instParms
and after the }
at the end of it. That, or the entire instParms
value really is meant to be a string, and the string hasn't been properly escaped (quotes preceded with backslashes, etc.).
Upvotes: 1