JSON使用gson解析错误 [英] JSON parse error using gson

查看:144
本文介绍了JSON使用gson解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正试图将数据解析到一个类中。

但问题是其中一个值是Json格式。

因此我得到一个MalformedJsonException。

这是Json字符串:

  {
name:Check on Server健康,
state:ABORTED,
startTime:1332962596131,
triggeredBy:GUI_MANUAL,
completionPct:25,
currentStep:sayHello,
processDefId:SW21SW,
jobRetries:0,
businessKey:-1,
comments: 无法实例化类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
}




Thi s是我使用的bean类:

  import com.google.gson.JsonElement; 

公共类WfActive {

私人字符串名称;
私有字符串状态;
private String startTime;
私人字符串状态;
private String endTime;
私人字符串评论;
private String triggeredBy;
private String execId;
私有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;
}

}

但是我得到一个格式错误的异常:

  LA,instParms:{message:start-workf // this is within instParms 

我的错误是什么,如何改正它?

请原谅大代码,有趣的部分是instParms

解决方案

您的JSON数据格式不正确(<
$ b

 mtId:MOTOROLA,
instParms:{message:start-workflow,sender: CLI_APP, 接收器: BPM_ENG, 参数:{ workflowId: SW21SW, mikesname: Mikeeeeeeey, wf_HostName: 本地主机, triggeredBy: GUI_MANUAL, replyQueue:temp-queue:// ID:SW-Demo01-51605-1332362748085-0:2246:1},userId:Ab,mtId:MOTOROLA,messageType:MESSAGE_MSG },
execId:292,

instParms 的值应该被转义。或者,可以使用si如果你知道单引号没有用在这个值中,那么可以使用引号。



这是有效的:

 mtId:MOTOROLA,
instParms:'{message:start-workflow,sender:CLI_APP,receiver: BPM_ENG, 参数:{ workflowId: SW21SW, mikesname: Mikeeeeeeey, wf_HostName: 本地主机, triggeredBy: GUI_MANUAL, replyQueue:TEMP-队列: // ID:SW-Demo01-51605-1332362748085-0:2246:1},userId:Ab,mtId:MOTOROLA,messageType:MESSAGE_MSG}',
execId:292,

所以是这样的:

 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\:\ \\mtId \:\MOTOROLA \,\messageType \:\MESSAGE_MSG \},
execId:292 ,

另一种方法是嵌入 instParms

 mtId:MOTOROLA,
instParms作为子对象而不是$ b> :{
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,
pre>

这可以说是最好的解决方案(除非有一些其他令人信服的理由来保持字符串表示形式)。



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"

解决方案

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).

这篇关于JSON使用gson解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆