JSON parse and deal with unexpected non-whitespace character - Javascript JSon

Javascript examples for JSon:JSon stringify

Description

JSON parse and deal with unexpected non-whitespace character

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  www.j  av a  2s .  com
var eventdata = '"{"error":"Test Wrong account"}"';
var message;
if (eventdata.slice(0,1) == '"') {
message = JSON.parse(eventdata.slice(1,-1));
    console.log('sliced! ' + message['error']);
} else {
    message = JSON.parse(eventdata);
    console.log('not sliced! ' + message['error']);
}
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials