메뉴 건너뛰기

프로그램언어

2014.03.26 02:12

JSON and JavaScript usage

조회 수 19064 추천 수 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 1598
219 사업자번호로 사업자 종류알기 2019.01.08 1222
218 디비내용을 엑셀 파일로 다운로드 시키는 방법 2019.01.08 1393
217 php/asp에서 가상번호 부여와 가상번호를 거꾸로 적용 2019.01.08 1429
216 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1808
215 배열을 테이블로 만들기 2019.01.08 1627
214 include 와 namespace 2019.01.08 1099
213 파일 2019.01.08 1226
212 디렉토리의 제어 2019.01.08 1222
211 PHP 문자열에서 검색어를 기준으로 앞뒤로 일정 길이만큼 자르기 2018.10.27 3539
210 PHP 랜덤확률 구하기 2018.10.27 4764
209 PHP 소켓을 이용하여 URL의 응답결과를 문자열로 받기 2018.10.27 3502
208 PHP 랜덤 문자열 생성 2018.10.27 4121
207 PHP XML 문서파싱 (SAX 방식 , DOM 방식) file 2018.10.27 3585
206 PHP split()와 explode()의 차이점 2018.10.27 3537
205 PHP eregi가 빠를까, strpos가 빠를까? 2018.10.27 4091
204 PHP 확장 모듈을 이용한 C 라이브러리 사용 2018.10.27 3790
203 자바스크립트 이스케이프 문자열을 PHP로 디코딩 하기 2018.10.27 3259
202 이미지 땡겨와서 출력하기 2018.09.28 5288
201 DB 연동 4단 셀렉트 박스 2018.09.28 6085
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved