메뉴 건너뛰기

프로그램언어

2014.03.26 02:12

JSON and JavaScript usage

조회 수 19066 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
JSON
  1. JavaScript Object Notation
  2. A Simple format designed to exchange data between different programming language
JSON Objects
  1. Creating with JavaScript

var JSONstring = 
	{
	    "firstname": "Greg", 
	    "email": "greg@fake_email.com",
	    "hobby": 
	    [
		{
		    "hobbyName": "sport", 
		    "isHobby": "true"
		},
		{
			"hobbyName": "reading", 
			"isHobby": "true"
		},
		{
			"hobbyName": "music", 
			"isHobby": "false"
		}
	    ]
	};
  1. Accessing with JavaScript

JSONstring.hobby[1].isHobby; // true
Creating JavaScript Objects
  1. JavaScript object <-> JSON string : http://www.json.org/json2.js
  1. Example

	<html>
	<head><TITLE>ditio.net jSon Tutorial</TITLE>
	<script src="http://www.json.org/json2.js"></script>
	<script>
	// JavaScript source code will be here
	function validate()
	{
	    var p = document.forms['personal'];
	 
	    var JSONObject = new Object;
	    JSONObject.firstname = p['firstname'].value;
	    JSONObject.email = p['email'].value;
	    JSONObject.hobby = new Array;
	 
	    for(var i=0; i<3; i++)
	    {
		JSONObject.hobby[i] = new Object;
		JSONObject.hobby[i].hobbyName = p['hobby'][i].value;
		JSONObject.hobby[i].isHobby = p['hobby'][i].checked;
	    }
	 
	    JSONstring = JSON.stringify(JSONObject);
	    runAjax(JSONstring);
	 
	}
	</head>
	<body>
	<form name="personal" action="" method="POST">
	Name <input type="text" name="firstname"><br>
	Email <input type="text" name="email"><br>
	Hobby 
		<input type="checkbox" name="hobby" value="sport"> Sport
		<input type="checkbox" name="hobby" value="reading"> Reading
		<input type="checkbox" name="hobby" value="music"> Music
	<input type="button" name="valid" value="Validate" onclick="validate()">
	</form>
	</body>
	</html>
Sending JSON object to PHP with AJAX
  1. Example

var request;
function runAjax(JSONstring)
{
    // function returns "AJAX" object, depending on web browser
    // this is not native JS function!
    request = getHTTPObject();
    request.onreadystatechange = sendData;
    request.open("GET", "parser.php?json="+JSONstring, true);
    request.send(null);
}
	 
// function is executed when var request state changes
function sendData()
{
    // if request object received response
    if(request.readyState == 4)
    {
	// parser.php response
	var JSONtext = request.responseText;
	// convert received string to JavaScript object
	var JSONobject = JSON.parse(JSONtext);
	 
	// notice how variables are used
	var msg = "Number of errors: "+JSONobject.errorsNum+
			"
- "+JSONobject.error[0]+
			"
- "+JSONobject.error[1];

	alert(msg);
    }
}

List of Articles
번호 제목 날짜 조회 수
220 배열을 테이블로 만들기 2019.01.08 1628
219 이미지 사이즈 비율로 조정하기 2019.01.08 1631
218 JAVASCRIPT 차트, 그래프 모음 (php chart, graph ) 2021.03.26 1632
217 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1813
216 dddotag - 허용하지 않는 태그 걸러내기 2019.01.16 1853
215 금액 단위를 만단위부터 표시하는방법 2019.01.16 1918
214 PHP 외부 XML 파싱 하기 2019.06.24 1949
213 메모장소스 2019.01.08 2002
212 PHP http 를 https 로 전환(redirect), http->https 2019.02.19 2185
211 날짜/시간함수 정리 2018.08.29 2429
210 PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype() 2018.08.29 2489
209 PHP에서 모든 세션 정보를 화면에 출력하는 방법 2018.08.29 2696
208 자바스크립트 이스케이프 문자열을 PHP로 디코딩 하기 2018.10.27 3260
207 PHP 소켓을 이용하여 URL의 응답결과를 문자열로 받기 2018.10.27 3504
206 PHP split()와 explode()의 차이점 2018.10.27 3538
205 PHP 문자열에서 검색어를 기준으로 앞뒤로 일정 길이만큼 자르기 2018.10.27 3541
204 PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지 2018.09.14 3553
203 PHP XML 문서파싱 (SAX 방식 , DOM 방식) file 2018.10.27 3588
202 PHP 확장 모듈을 이용한 C 라이브러리 사용 2018.10.27 3792
201 한글이 깨져서 나올 때 - iconv 2018.08.29 3934
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved