메뉴 건너뛰기

프로그램언어

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);
    }
}

  1. PHP 에서의 소켓(Socket) 통신

    Date2014.04.12 Views20395
    Read More
  2. PHP 세션에서 시작, 변수등록, 변수삭제, 부수기

    Date2014.03.26 Views19490
    Read More
  3. 어떤 스마트폰으로 접속했는지 알아내는 소스 , (안드로이드 아이폰 )

    Date2014.03.26 Views17716
    Read More
  4. PHP 소스 : 이미지 리사이즈, 섬네일

    Date2014.03.26 Views20704
    Read More
  5. 네이버 지도 api php버전

    Date2014.03.26 Views19434
    Read More
  6. Drag/Drop HTML elements

    Date2014.03.26 Views19535
    Read More
  7. 이미지 워터마크 구현

    Date2014.03.26 Views19667
    Read More
  8. 특수문자 없애는 정규표현식

    Date2014.03.26 Views21623
    Read More
  9. jQuery 개발자를 위한 메모 - 레퍼런스

    Date2014.03.26 Views19522
    Read More
  10. JSON and JavaScript usage

    Date2014.03.26 Views19064
    Read More
  11. jQuery 개발자를 위한 메모 - 플러그 인

    Date2014.03.26 Views19257
    Read More
  12. 웹에서 Excel 로 출력하기

    Date2014.03.26 Views20356
    Read More
  13. 웹페이지 프린트 하기 html 수준

    Date2014.03.26 Views19805
    Read More
  14. [PHP] 한글명 파일 다운로드받기

    Date2014.03.26 Views20551
    Read More
  15. FPDF - PHP로 PDF 만들기

    Date2014.02.27 Views21075
    Read More
  16. 해당하는 날짜가 그달의 몇주째인지 계산

    Date2014.02.27 Views26351
    Read More
  17. php 파일 다운로드 구현

    Date2014.02.27 Views19794
    Read More
  18. php 파일 확장자

    Date2014.02.27 Views20234
    Read More
  19. MYSQL 업데이트 두 번 하기

    Date2014.02.27 Views19729
    Read More
  20. mysql 에러 구문 표시

    Date2014.02.27 Views20349
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved