简体   繁体   English

实现此JSON.parse的正确方法是什么?

[英]what is the proper way to implement this JSON.parse?

I'm required to use the plain vanilla JSON.parse built into js. 我需要使用js中内置的普通香草JSON.parse。 This is the xmlhttp.responseText returned from the service that I'm calling with xhr: 这是我使用xhr调用的服务返回的xmlhttp.responseText:

"[{\"Id\":1071,\"Title\":\"Scheduled Maintenance\",\"Summary\":\"Site will be unavailable Sunday, August 7th between 9:00AM and 10:00AM MST.\",\"Severity\":\"Informational\",\"ApplicationsImpacted\":[1068],\"IsPubliclyViewable\":true,\"IsDismissable\":true}]"

JSON.parse() does not convert this string into an object. JSON.parse()不会将此字符串转换为对象。 However, JSON.parse() does convert this string into an object if I write my js like this: 但是,如果我这样编写js,JSON.parse()会将此字符串转换为对象:

JSON.parse(JSON.parse(xmlhttp.responseText))

This seems to be a hack. 这似乎是一个hack。 Can you recommend a cleaner way to convert this string into an object? 您能推荐一种更干净的方法将此字符串转换为对象吗?

It is working fine just remove the extra quotes(") in the beginning and ending of the string. 只需删除字符串开头和结尾的多余引号(“),它就可以正常工作。 在此处输入图片说明

JSON.parse("[{\"Id\":1071,\"Title\":\"Scheduled Maintenance\",\"Summary\":\"Site will be unavailable Sunday, August 7th between 9:00AM and 10:00AM MST.\",\"Severity\":\"Informational\",\"ApplicationsImpacted\":[1068],\"IsPubliclyViewable\":true,\"IsDismissable\":true}]")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM