4

I have a string like this:

{"Restriction":"<wbr><a href=\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\" 
target=\"_blank\"><span style=\"color: rgb(0, 0, 205);\">more info</span></a></wbr>"}

but I can't parse it with JSON.parse. My code looks like this:

var s = '{"Restriction":"<wbr><a href=\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\" target=\"_blank\"><span style=\"color: rgb(0, 0, 205);\">more info</span></a></wbr>"}';
var obj = JSON.parse(s);

and I got the error:

Uncaught SyntaxError: Unexpected token.

My guess is 「\"」 made something wrong, but I can't change the string because I got it from a call to a remote API. Here's my code:

// We need this to build our post string
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');

function PostCode(codestring) {

  // An object of options to indicate where to post to
  var post_options = {
      host: 'api.domain',
      port: '80',
      path: '/webservice/service.asmx/method?key=123456',
      method: 'GET',
      headers: {
          'Content-Type': 'text/plain'
      }
  };

  // Set up the request
  var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
        var x = {};
          console.log('Response down');
          x = JSON.parse(chunk);
      });
  });

  post_req.end();

}
PostCode();
1
  • I got it from a call to a remote API. How it can be truth? I think, you're copypasted API answer and use as-is in your code.. Your string actualy doesn't have backslashes, replace can't to help here. Commented Nov 8, 2015 at 16:46

4 Answers 4

5

It's not a valid JSON. Backslashes should be escaped too.

var s = '{"Restriction":"<wbr><a href=\\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\\" target=\\"_blank\\"><span style=\\"color: rgb(0, 0, 205);\\">more info</span></a></wbr>"}';
JSON.parse(s); // correct

I think, you should post bug report to this remote API.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't to parse the chunk of data, you need to load all.

  var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      var json = '';
      res.on('data', function (chunk) {
        // Why this arg called chunk? That's not all data yet
        json += chunk;
      });
      res.on('end', function(){
         // Here we get it all
         console.log(JSON.parse(json));
      });

  });

Comments

1

You can use replace:

var s = '{"Restriction":"<wbr><a href=\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\" target=\"_blank\"><span style=\"color: rgb(0, 0, 205);\">more info</span></a></wbr>"}';
console.log(s);
console.log(s.replace(/\"/g, ""));

Comments

0

To parse those html attributes you would need to double escape the quotes: \\" because they are two layers down. Or, preferably maybe, use single quotes for the attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.