메뉴 건너뛰기

프로그램언어

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 PHP 쉘 스크립트 2021.03.26 470
219 php 암호화 복호화 , 간단한 암호화 2023.01.12 366
218 PHP 에서의 소켓(Socket) 통신 2014.04.12 20395
217 php 엑셀 다운로드 구현 2017.03.07 29787
216 PHP 외부 XML 파싱 하기 2019.06.24 1944
215 PHP 이미지 리사이즈 함수 imagecopyresized 2023.01.12 212
214 php 이미지 리사이징 image resizing 2023.01.12 259
213 PHP 특정 디렉토리에 있는 파일 갯수 구하기 2018.07.19 5455
212 PHP 파일 다루기 2015.04.14 22401
211 php 파일 다운로드 구현 2014.02.27 19794
210 php 파일 다운로드 구현 2014.04.12 21699
209 PHP 파일 업로드 FORM 처리 2023.01.12 229
208 php 파일 확장자 2014.02.27 20234
207 PHP 파일크기 단위 붙이기 (용량 변환) file size conversion source code 2018.07.04 5793
206 PHP 하위 디렉토리 포함 디렉토리 리스트 출력 2023.01.12 236
205 PHP 확장 모듈을 이용한 C 라이브러리 사용 2018.10.27 3786
204 PHP 휴대폰번호 짜르기 (preg_replace) "-" 넣기. 형식바꾸기 2018.07.04 4057
203 PHP, $_SERVER 변수 2017.04.13 19505
202 php/asp에서 가상번호 부여와 가상번호를 거꾸로 적용 2019.01.08 1429
201 PHPExcel 클래스를 이용해 Excel 2007~2010 의 xlsx 파일 읽기 (100만 행 까지) 2017.03.06 21695
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved