【问题标题】:Convert string in JSON在 JSON 中转换字符串
【发布时间】:2015-11-19 01:02:55
【问题描述】:

嘿,我想把这个字符串转换成 JSON:

var newPart = '{\'' + name + '\': {\'content-type\' : ' +  type + ', \'content\': ' + content +  ',\'type\': \'content\'}}';

结果给出以下字符串:

{'partHtml': {'content-type' : text/html, 'content': dfg,'type': 'content'}}

我想从此字符串中获取 JSON 对象,我尝试了以下操作,但失败并出现错误:"SyntaxError: Unexpected token '"

newPart = JSON.parse(newPart);

我尝试不使用 ' 进行一些更改,但问题仍然存在。

感谢您的帮助。

【问题讨论】:

  • 将所有单引号改为双引号。
  • 所有字符串必须用双引号引起来 { "partHtml": { "content-type": "text/html", "content": "dfg", "type": "content" } }
  • 您的字符串不代表 JSON。请参阅 json.org 的 JSON 规范

标签: javascript arrays json parsing stringify


【解决方案1】:

虽然 Lorenzo 的回答是正确的,但你为什么要这样构建字符串是有原因的吗?为什么不直接构建对象呢?

var type = 'text/html';
var content = 'dfg';
var name ='partHtml'

var newPart = {};
newPart[name] = {content_type: type, content: content, type: 'content'};

【讨论】:

    【解决方案2】:

    JSON 字符串使用" 引用,而不是'
    因此,您的示例应该是:

    var newPart = '{"' + name + '": {"content-type" : ' +  type + ', "content": ' + content +  ',"type": "content"}}';
    newPart = JSON.parse(newPart);
    

    或者,如果您已经拥有所需的一切,您可以立即构造对象,而无需使用JSON.parse()

    var newPart = {"name": {"content-type": type, "content": content, "type": "content"}};
    

    【讨论】:

      【解决方案3】:

      这是一种非常糟糕的做法,也是一种不安全的方法。你只需要创建一个对象。

      var newPart = {     
          "content-type" : "text/html",
          "content": "dfg",
          "type": "content"      
      }
      

      【讨论】:

      • 您在此处创建的对象与 OP 需要的对象不同。您遗漏了“partHtml”键。
      • 无论这个对象看起来或包含什么,这是最有效和最安全的方法。
      • 这不是答案 :)
      • 好的,我错过了对象的第一个键。请考虑将 mikeecb 的答案视为最佳答案。
      • 是的,mikeecb 的回答是正确的,你可能想删除这个错误的
      【解决方案4】:

      这不是一个有效的 JSON,因此无法解析它。 一个有效的版本应该是这样的:

      {"partHtml": {"content-type": "text/html", "content": "dfg", "type": "content"}}
      

      你不能使用 ' 字符,你需要 " 代替。

      【讨论】:

        【解决方案5】:

        JSON 字符串使用 "

        引用

        `var newStr = '{"' + name + '": {"content-type" : "' + type + '", "content": "' + content + '","type": "content "}}';

        var newJson = JSON.parse(newStr);

        var newJson = eval('(' + newStr + ')');

        【讨论】:

          【解决方案6】:

          只需将此行替换为您的

          var newPart = '{\"' + name + '\": {\"content-type\" : \"' +  type + '\", \"content\": \"' + content +  '\",\"type\": \"content\"}}';
          

          【讨论】:

          • 我做了同样的问题..这就是我现在的结果,但仍然是同样的问题:{'partHtml': {'content-type': 'text/html', 'content ' : 'fgh','type': '内容'}}
          【解决方案7】:

          在合并字符串时必须小心。我建议不要创建字符串并面临分隔问题,而是创建一个对象实例并动态分配属性。

          var newPart = {};
          newPart.name = {content : content};
          newPart.name['content-type'] = type;
          

          等等..

          【讨论】:

            【解决方案8】:

            您的 JSON 中有错误。 text/html 是一个字符串,但它缺少引号。

            尝试添加它们:

            var newPart = '{"' + name + '": {"content-type" : "' +  type + '", "content": "' + content +  '", "type": "content"}}'
            

            请注意,如果您想要一个 lint 有效的 JSON,您应该使用双引号。在任何在线工具中测试您的 JSON,例如 http://jsonlint.com/

            {
                "partHtml": {
                    "content-type" : "text/html",
                    "content": "dfg",
                    "type": "content"
                }
            }
            

            此外,如果您的变量包含双引号,则在解析 JSON 时会出错。这个答案适用于您的代码,但我不建议在实际应用程序中使用它,因为这是一种不好的做法。为了获得最佳实践,请查看 mikeecb 对我的回答所做的补充。

            【讨论】:

            • 我修复了这样的报价: {'partHtml': {'content-type' : 'text/html', 'content' : 'fgh','type': ' content'}} 但同样的错误:SyntaxError: Unexpected token '
            • 请再次检查,我编辑了答案并直接在您的代码中添加了修复程序。
            • 如果您的任何变量包含双引号怎么办?
            • 在这种情况下,解析时会出错。这个答案适用于 OP 的示例代码,但我不建议在实际应用程序中使用它,因为这是一种不好的做法。为了获得最佳实践,请查看 mikeecb 对我的回答所做的补充。感谢您指出,我会更新我的答案
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-23
            • 1970-01-01
            • 2018-06-28
            • 2016-07-13
            • 2012-10-26
            相关资源
            最近更新 更多