JavaScript JSON stringify() 메서드
예시
JavaScript 객체를 문자열화:
var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON =
JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
아래에서 더 많은 "직접 사용해 보기" 예를 살펴보세요.
정의 및 사용
JSON.stringify() 메서드는 JavaScript 객체를 문자열로 변환합니다.
웹 서버에 데이터를 보낼 때 데이터는 문자열이어야 합니다.
브라우저 지원
표의 숫자는 이 방법을 완전히 지원하는 첫 번째 브라우저 버전을 지정합니다.
Method | |||||
---|---|---|---|---|---|
stringify() | 4.0 | 8.0 | 3.5 | 4.0 | 11.5 |
통사론
JSON.stringify(obj, replacer, space)
매개변수 값
Parameter | Description |
---|---|
obj | Required. The value to convert to a string |
replacer | Optional. Either a function or an array used to transform the result. The replacer is called for each item. |
space | Optional. Either a String or a Number. A string to be used as white space (max 10 characters), or a Number, from 0 to 10, to indicate how many space characters to use as white space. |
기술적 세부 사항
반환 값: | 문자열 |
---|---|
자바스크립트 버전: | ECMA스크립트 5 |
더 많은 예
예시
교체 기능 사용 :
/*replace the value of "city" to upper case:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj, function
(key, value) {
if (key == "city") {
return value.toUpperCase();
} else {
return value;
}
});
예시
space 매개변수 사용 :
/*Insert
10 space characters for each white space:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj,
null, 10);
예시
space 매개변수 사용 :
/*Insert
the word SPACE for each white space:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj,
null, "SPACE");
관련 페이지
JSON 튜토리얼: JSON 소개