简体   繁体   English

使用gson的JSON解析错误

[英]JSON parse error using gson


I am trying to parse the data into a class. 我正在尝试将数据解析为一个类。
But the problem is that one of the values is in Json format. 但是问题在于值之一是Json格式。
Hence I am getting a MalformedJsonException. 因此,我得到了MalformedJsonException。
This is the Json string: 这是Json字符串:

{
    "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: 这是我正在使用的bean类:

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" 请原谅大代码,有趣的部分是“ instParms”

Your JSON data is malformed (middle line below): 您的JSON数据格式错误(下面的中线):

"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. instParms值中的第二个双引号应转义。 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: 另一种选择是将instParms的值作为子对象而不是stngng:

"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). 可以说这是最好的解决方案(除非还有其他令人信服的理由保留字符串表示形式)。

You can't readily fix it in the code consuming the JSON, nor should you. 您不能轻易地在使用 JSON的代码中对其进行修复,也应该如此。 You need to fix the other end, which is generating invalid JSON. 您需要修复另一端,即生成无效的JSON。 It looks like just an erroneous quote prior to the { on the value for instParms and after the } at the end of it. 看起来好像是instParms值的{之前和之后的}之后的错误引号。 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.). 那,或者整个instParms值实际上是一个字符串,并且该字符串尚未正确转义(引号前面加反斜杠等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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