user6604655 user6604655 - 3 months ago 5
Javascript Question

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

I'm required to use the plain vanilla JSON.parse built into js. This is the xmlhttp.responseText returned from the service that I'm calling with xhr:

"[{\"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. However, JSON.parse() does convert this string into an object if I write my js like this:

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


This seems to be a hack. Can you recommend a cleaner way to convert this string into an object?

Answer

It is working fine just remove the extra quotes(") in the beginning and ending of the string. enter image description here

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}]")
Comments