메뉴 건너뛰기

프로그램언어

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
번호 제목 날짜 조회 수
160 PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때... 2017.03.06 16610
159 PHP웹 보안 취약점 TOP5(웹해킹) 2023.01.12 292
158 PHP와 HTML과 자바스크립트의 관계 2021.03.26 311
157 PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지 2018.09.14 3548
156 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1807
155 PHP에서 조건문 처리 2015.04.14 22038
154 PHP에서 자바스크립트 값 가져오기 2014.02.27 31635
153 PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype() 2018.08.29 2465
152 PHP에서 암호화 encrypt 복호화 decrypt 해서 값을 넘기기 2018.02.09 10626
151 PHP에서 모든 세션 정보를 화면에 출력하는 방법 2018.08.29 2694
150 PHP에서 데이터를 엑셀(Excel)로 저장 2017.02.19 18428
149 PHP에서 UTF와 EUC-KR 변환 2019.02.19 1554
148 PHP에서 PDF파일 생성하기 2014.02.27 32777
147 PHP에서 Excel 파일을 만들 수 있는 PHPExcel file 2017.03.06 17112
146 PHP에서 CSV 파일 export file 2016.04.22 22335
145 PHP로 엑셀 자료 MySQL에 넣기 2017.03.06 17875
144 PHP로 Excel 파일 만들기... 2014.02.27 30257
143 php로 db 컨트롤 1 2017.03.06 15769
142 phpMyAdmin WebMysql 에 CSV 엑셀 파일 업로드 입력하기 ( Excel / Upload / data / 데이터 / 데이타 ) file 2021.03.25 760
141 phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성 file 2017.03.06 22787
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved