prosource

JSON.stringify가 객체의 내용 대신 [객체]를 반환합니다.

probook 2023. 2. 22. 22:18
반응형

JSON.stringify가 객체의 내용 대신 [객체]를 반환합니다.

여기에서는 JavaScript 객체를 생성하여 JSON 문자열로 변환합니다만,JSON.stringify돌아온다"[object Object]"이 경우 오브젝트의 내용을 표시하는 대신.JSON 문자열에 실제로 객체의 내용이 포함되도록 이 문제를 어떻게 회피할 수 있을까요?

var theObject = {name:{firstName:"Mark", lastName:"Bob"}};
alert(JSON.stringify(theObject.toString())); //this alerts "[object Object]"

사용하다alert(JSON.stringify(theObject));

theObject.toString()

.toString()메서드가 원인입니다.분리하면 바이올린이 작동합니다.http://jsfiddle.net/XX2sB/1/

이 경우 JSON.stringify는 "[객체]"를 반환합니다.

그것은 당신이 전화하고 있기 때문입니다.toString()개체를 직렬화하기 전에 다음 작업을 수행합니다.

JSON.stringify(theObject.toString()) /* <-- here */

를 삭제합니다.toString()정상적으로 동작합니다.

alert( JSON.stringify( theObject ) );

사용하다

var theObject = {name:{firstName:"Mark", lastName:"Bob"}};
alert(JSON.stringify(theObject));

언급URL : https://stackoverflow.com/questions/16493498/json-stringify-returns-object-object-instead-of-the-contents-of-the-object

반응형