Skip to content Skip to sidebar Skip to footer

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. Hence I am getting a MalformedJsonException. This is the Json string:

Solution 1:

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

Solution 2:

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

Post a Comment for "Json Parse Error Using Gson"