Javascript 如何将json转换为第二个json那样的有效json

Javascript 如何将json转换为第二个json那样的有效json,javascript,Javascript,期望: { "items": "{'Link': 'http://www.indiansalt.com/services/emp_add_form.asp', 'Title': 'Apply Online'}, {'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8522948124.pdf', 'Title': 'Notification '}, {'Link': 'http://ww

期望:

{
"items": 
  "{'Link': 'http://www.indiansalt.com/services/emp_add_form.asp', 'Title': 'Apply Online'},
  {'Link': 'media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8522948124.pdf', 'Title': 'Notification '},
  {'Link': 'http://www.indiansalt.com/', 'Title': ' Official Website'}"
}

如何将我的json转换为第二个json那样的有效json。

由于几个原因,它不是有效的json,因此,如果这确实是您使用的格式,您需要:

  • 将单引号替换为双引号
  • 将字符串值用括号括起来
  • 然后将该值解析为JSON值
  • 最后,如果希望最终以字符串格式显示,请将其字符串化
  • var值={
    “项目”:
    “{'Link':'http://www.indiansalt.com/services/emp_add_form.asp“,”Title“:”Apply Online“},{”Link“:”media/pdf/details/all india govt jobs/other all india govt jobs/8522948124.pdf“,”Title“:”Notification“},{”Link“:”http://www.indiansalt.com/“,”标题“:”官方网站“}”
    }
    //用双引号替换单引号
    theValue.items=theValue.items.replace(/'/g',“);
    //用括号括起来
    theValue.items='['+theValue.items+'];
    //解析
    theValue.items=JSON.parse(theValue.items);
    //如果需要字符串格式,请使用stringify
    var result=JSON.stringify(值为null,1);
    
    console.log(result);
    第二个是无效的json。我相信有效的json会有一个数组用于
    “items”
    字段。请在浏览器中进行编辑,它是这样的:“更多信息”:“[\n{\n\“Link\”:\”,\n\“Title\”:“Apply Online\”,\n{\n\“Link\”:“Notification”、\n\“Title\:“Notification”“Notification”、\n\“Link\”:\“\”,\n\“Title\:\“官网\”\n}\n]”
    {
    "items": 
      [{"Link": "http://www.indiansalt.com/services/emp_add_form.asp", "Title": "Apply Online"},
      {"Link": "media/pdf/details/all-india-govt-jobs/other-all-india-govt-jobs/8522948124.pdf", "Title": "Notification "},
      {"Link": "http://www.indiansalt.com/", "Title": "Official Website"}]
    }