javascript 如何 JSON.parse 带有 HTML 标记的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33595331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:40:01  来源:igfitidea点击:

How do I JSON.parse a string with HTML tag?

javascripthtmljson

提问by Marsen Lin

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:

但我无法用 JSON.parse 解析它。我的代码如下所示:

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:

我的猜测是「\"」出了点问题,但我无法更改字符串,因为我是从远程 API 调用中得到的。这是我的代码:

// 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();

采纳答案by vp_arth

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));
      });

  });

回答by vp_arth

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

它不是有效的 JSON。反斜杠也应该转义。

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.

我认为,您应该向 this 发布错误报告remote API

回答by andrrs

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.

要解析这些 html 属性,您需要对引号进行双重转义: \\",因为它们向下两层。或者,最好对属性使用单引号。

回答by Brane

You can use replace:

您可以使用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, ""));