Synaxerror json.parse unexpected character in row 1 column 1 json data

I donโ€™t know what the problem is, I got the syntax error in json.parse above. I use the code below

Storage.prototype.setObject = function(key, value) { this.setItem(key, JSON.stringify(value)); } Storage.prototype.getObject = function(key) { var value = this.getItem(key); return value && JSON.parse(value); } function main() { var data = { "a":"something1", "b":"something2" }; sessionStorage.setObject('data',data); var newData = sessionStorage.getObject('data'); console.log(newData); } 

when calling getObject ('data') I got an error in "firefox", while "no error" in chrome pls helps me figure out the problem, I run the code on the sample separately, and it works fine for me, but in my to a project where im does something like this, it causes an error.

+6
source share
1 answer

I get no errors in either Firefox or Chrome. However, you can catch this exception for debugging by adding a try/catch to the getObject method

 Storage.prototype.getObject = function(key) { var value = this.getItem(key); if (value) { try { value = JSON.parse(value); } catch (err) { console.error("Error parsing stored data: " + err); } } } 
+1
source

Source: https://habr.com/ru/post/969777/


All Articles