Syntax error when parsing JSON string
Solution 1
The data is not valid JSON, since \"
in strings seems to have been replaced with "\
.
Contact the author of that supposedly-JSON and notify him or her that there are plenty of JSON libraries available for all languages and platforms.
Solution 2
storejson= getJSonObject("@ViewBag.JsonData");
function getJSonObject(value) {
return $.parseJSON(value.replace(/"/ig, '"'));
}
Solution 3
I might be mistaken here but I think it's due to the JSON data itself it doesn't work. The JSON parser probably chokes on the double-quotes in the HTML contained in the JSON'ed string variable. I think it will work when you pre-process the HTML string before outputting the JSON data so you change the double-quotes into something else. ( and do the other way around after parsing the JSON data )
An example might clarify a bit:
instead of this:
{"html": { "#data": "<input name="somename" type="text"/>"} }
i'd try to use this:
{"html": { "#data": "<input name="somename" type="text"/>"} }
Hope it helps...
Solution 4
JSON = Javascript Object Notation, therefor the value of the "#data"
property is an invalid string.
You can validate your JSON here
mike_hornbeck
Updated on April 10, 2020Comments
-
mike_hornbeck about 4 years
I have a sample JSON with some part of my webpage rendered :
{"html": {"#data": "\n<h2>Data</h2>\n<div class="\"manufacturer-image\"">\n \n</div>\n <form action="\"/manage/update-manufacturer-data/3\"" method="\"post\"">\n \n <div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_name\"">Nazwa</label>:\n </div>\n \n \n <div class="\"error\"">\n <input id="\"id_name\"" name="\"name\"" maxlength="50" type="\"text\"">\n <ul class="\"errorlist\""><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n <div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_image\"">Zdjecie</label>:\n </div>\n \n \n <div>\n <input name="\"image\"" id="\"id_image\"" type="\"file\"">\n </div>\n \n </div>\n\n <div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_description\"">Opis</label>:\n </div>\n \n \n <div>\n <textarea id="\"id_description\"" rows="10" cols="40" name="\"description\""></textarea>\n </div>\n \n </div>\n \n <div class="\"buttons\"">\n <input class="\"ajax-save-button" button\"="" type="\"submit\"">\n </div>\n</form>"}}
This string is returned with ajax call and jQuery 1.6.1 throws an error :
SyntaxError: JSON.parse: expected ',' or '}' after property value in object
in the following part of the code :
parseJSON: function( data ) { if ( typeof data !== "string" || !data ) { return null; } // Make sure leading/trailing whitespace is removed (IE can't handle it) data = jQuery.trim( data ); // Attempt to parse using the native JSON parser first if ( window.JSON && window.JSON.parse ) { console.warn('data: ', data); var ret; try{ ret = window.JSON.parse(data); } catch(e){ ret = {}; console.warn(e); } return ret; //return window.JSON.parse( data ); }
What am I missing here ?
EDIT:
I have parsed through the previous 'json' (which by the way was created with python's simplejson lib, so I wonder how can this be working anywhere) and now jsonlint shows, that I have proper JSON. Still the problem remains the same. The new string :
{"html": [{"#data": "\n<h2>Data</h2>\n<div class="manufacturer-image">\n \n</div>\n<form action="/manage/update-manufacturer-data/4" method="post">\n \n <div class="field">\n <div class="label">\n <label for="id_name">Nazwa</label>:\n </div>\n \n \n <div class="error">\n <input id="id_name" type="text" name="name" maxlength="50" />\n <ul class="errorlist"><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n <div class="field">\n <div class="label">\n <label for="id_image">Zdjecie</label>:\n </div>\n \n \n <div>\n <input type="file" name="image" id="id_image" />\n </div>\n \n </div>\n\n <div class="field">\n <div class="label">\n <label for="id_description">Opis</label>:\n </div>\n \n \n <div>\n <textarea id="id_description" rows="10" cols="40" name="description"></textarea>\n </div>\n \n </div>\n \n <div class="buttons">\n <input type="submit" class="ajax-save-button button" />\n </div>\n</form>"}]}
EDIT2: Ok it looks, that JSOn leaving my backend is proper but dumb jQuery adds additional quotes around each '"' which is kinda odd.