Excluded double quotes in JSON string causing error when parsing

I am using GSON to convert List (Map (String, String)) to json string and pass it to interface before session. GSON correctly escapes double qoutes with / ", but the problem occurs when parsing the json string.

The generated JSON string is -

[{"queueList": [{"Name": "Queue\"1\""}, {"Name": "Queue2"}, {"Name": "Queue3"}, {"Name": "Queue4"}]}]


Mistake. I get chrome while parsing: "Not expected number"

The code I am using to parse is given below

 $(document).ready(function () {
var menuItemsStr = '[{"queueList": [{"Name": "Queue\"1\""}, {"Name": "Queue2"}, {"Name": "Queue3"}, {"Name": "Queue4"}]}]';
var menuItems = $.parseJSON(menuItemsStr);
$.each(menuItems[0].queueList, function (idx, obj) {
    var listItemHTML = $("#listItem").clone().html();
    listItemHTML = listItemHTML.replace(/\@QN/g, obj.Name);



Below is fiddle link where above code is present with issue http://jsfiddle.net/vinaybvk/qvwL9246/2/

When the JSON string has \\ ", then the fiddle works fine as expected.
The working string with escaped JSON is below:

var menuItemsStr = '[{"queueList": [{"Name": "Queue\\"1\\""}, {"Name": "Queue2"}, {"Name": "Queue3"}, {"Name": "Queue4"}]}]';


I can't find a way to add \\ before "to get the behavior to work. I'm trying in both java and javascript.
What I'm trying to do in javascript is in the above script in the comments
what I'm trying to do in java. it is str.replaceAll ("\" "," \\\\\ ")) which generates a string with \\\"

Please let me know. Is there a way to get this fix or am I doing something wrong.



source to share

1 answer

Instead of quoting the string, then parsing it, in this case you can simply do:

var menuItems = <s:property value="#session['jsonFormattedResult']" escapeHtml = "false" />;


which results in a valid object.



All Articles