无法从 api 响应中解析 json

Unable to parse json from api response

我能够在控制台中看到清晰的响应。

{'timestamp': '2021-12-07 09:54:01.195543', 'Operating Parameters': {'quality_control': 'Action Needed', 'tool_status': 'running', 'message': {'Resurvey': {'Survey Environment': 'Flow higher than threshold', 'Recommended Action': 'Modify pumps shutoff sequence'}}}, 'Sensor Data': {'bit_depth': '772', 'inclination': '37.83', 'azimuth': '299.86', 'gravity_toolface': '11.43', 'survey': {'survey_counter': '140', 'survey_time': '2021-12-07 09:54:01.195520'}, 'previous_survey': {'previous_survey_depth': '1111', 'previous_survey_time': '2021-12-07 09:54:01.195539'}}}

但是当我尝试获取 timestamp 它的打印在控制台中未定义时

  console.log("timestamp", JSON.parse(JSON.stringify(jobDetails)).timestamp);

问题似乎是JSON.parse doesn't accept single qoutes. The best way to solve this is to change your backend implementation to something compliant with the JSON spec。如果那不可能,您可以这样做,但请注意,这是一个非常脆弱的解决方案。

const jobListings = "{'timestamp': '2021-12-07 09:54:01.195543', 'Operating Parameters': {'quality_control': 'Action Needed', 'tool_status': 'running', 'message': {'Resurvey': {'Survey Environment': 'Flow higher than threshold', 'Recommended Action': 'Modify pumps shutoff sequence'}}}, 'Sensor Data': {'bit_depth': '772', 'inclination': '37.83', 'azimuth': '299.86', 'gravity_toolface': '11.43', 'survey': {'survey_counter': '140', 'survey_time': '2021-12-07 09:54:01.195520'}, 'previous_survey': {'previous_survey_depth': '1111', 'previous_survey_time': '2021-12-07 09:54:01.195539'}}}"
const jsonJobListings = jobListings.replace(/'/g, '"')
const objJobListings = JSON.parse(jsonJobListings)
console.log(objJobListings.timestamp) // "2021-12-07 09:54:01.195543"