메뉴 건너뛰기

프로그램언어

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

  1. No Image 23Dec
    by
    2016/12/23 Views 23852 

    $_FILES

  2. No Image 23Dec
    by
    2016/12/23 Views 23949 

    $_SERVER 함수

  3. No Image 21Sep
    by
    2016/09/21 Views 33246 

    $_SERVER 환경변수

  4. No Image 27Feb
    by
    2014/02/27 Views 24456 

    $_SERVER변수

  5. 13자리 timestamp 생성하기

  6. No Image 23Dec
    by
    2016/12/23 Views 23089 

    addslashes — 문자열을 슬래시로 인용

  7. No Image 14Apr
    by
    2015/04/14 Views 24258 

    addslashes 함수의 필요성

  8. ajax refresh 시키기(자동리플래쉬) with php

  9. No Image 06Mar
    by
    2017/03/06 Views 21042 

    Ajax로 구연한 실시간 서버시간출력

  10. No Image 26Mar
    by
    2021/03/26 Views 361 

    AJAX로 해당 페이지에서 COOKIE 사용하기

  11. No Image 04Jul
    by
    2018/07/04 Views 8469 

    AJAX를 활용하여 JSON 댓글 처리하기 (PHP)

  12. No Image 14Apr
    by
    2015/04/14 Views 24911 

    array (배열)

  13. No Image 23Dec
    by
    2016/12/23 Views 22216 

    array_key_exists 배열에서 key가 존재하는지 확인

  14. No Image 23Dec
    by
    2016/12/23 Views 21611 

    array_push 배열 끝에 하나 이상의 요소를 추가

  15. No Image 23Dec
    by
    2016/12/23 Views 20782 

    array_slice 배열의 일부를 추출

  16. base64 인코딩/디코딩 함수의 특징

  17. No Image 23Dec
    by
    2016/12/23 Views 21324 

    call_user_func 사용자가 정의한 함수를 호출하여 실행고자 할 때 사용

  18. No Image 23Dec
    by
    2016/12/23 Views 19893 

    class_exists 클래스가 정의되었는지 확인

  19. No Image 27Feb
    by
    2014/02/27 Views 30511 

    Class를 이용한 DB Connection 소스 (Oracle, MyS

  20. No Image 29Mar
    by
    2021/03/29 Views 498 

    CodeIgniter - DB오류체크, 디버깅 여부 설정

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved