Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

javascript - Difference between pm.response.json() and JSON.parse(responseBody)

What is the fundamental difference between

const json_response = pm.response.json()

and

json_response = JSON.parse(responseBody)
question from:https://stackoverflow.com/questions/65901110/difference-between-pm-response-json-and-json-parseresponsebody

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Danny Dainton has already pointed out, JSON.parse(responseBody) and pm.response.json() do the same thing - they both return the response body of your API request as a string in JSON format.

Concerning the missing const declaration in the second statement. - If you put the two statements in the same file, then clearly you are correct to leave out the const declaration in the second statement.
(Otherwise there will be a syntax error: Identifier 'json_response' has already been declared.)

But since the two statements do the same thing, it makes no sense to have them both in the same script. And if they are not in the same script, a declaration is necessary in the second statement as well as in the first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...