메뉴 건너뛰기

프로그램언어

2014.03.26 02:12

JSON and JavaScript usage

조회 수 19068 추천 수 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. $_FILES

    Date2016.12.23 Views23853
    Read More
  2. $_SERVER 함수

    Date2016.12.23 Views23949
    Read More
  3. $_SERVER 환경변수

    Date2016.09.21 Views33246
    Read More
  4. $_SERVER변수

    Date2014.02.27 Views24456
    Read More
  5. 13자리 timestamp 생성하기

    Date2020.09.28 Views658
    Read More
  6. addslashes — 문자열을 슬래시로 인용

    Date2016.12.23 Views23089
    Read More
  7. addslashes 함수의 필요성

    Date2015.04.14 Views24258
    Read More
  8. ajax refresh 시키기(자동리플래쉬) with php

    Date2017.03.06 Views23188
    Read More
  9. Ajax로 구연한 실시간 서버시간출력

    Date2017.03.06 Views21042
    Read More
  10. AJAX로 해당 페이지에서 COOKIE 사용하기

    Date2021.03.26 Views361
    Read More
  11. AJAX를 활용하여 JSON 댓글 처리하기 (PHP)

    Date2018.07.04 Views8470
    Read More
  12. array (배열)

    Date2015.04.14 Views24911
    Read More
  13. array_key_exists 배열에서 key가 존재하는지 확인

    Date2016.12.23 Views22216
    Read More
  14. array_push 배열 끝에 하나 이상의 요소를 추가

    Date2016.12.23 Views21612
    Read More
  15. array_slice 배열의 일부를 추출

    Date2016.12.23 Views20782
    Read More
  16. base64 인코딩/디코딩 함수의 특징

    Date2018.02.09 Views13092
    Read More
  17. call_user_func 사용자가 정의한 함수를 호출하여 실행고자 할 때 사용

    Date2016.12.23 Views21324
    Read More
  18. class_exists 클래스가 정의되었는지 확인

    Date2016.12.23 Views19894
    Read More
  19. Class를 이용한 DB Connection 소스 (Oracle, MyS

    Date2014.02.27 Views30511
    Read More
  20. CodeIgniter - DB오류체크, 디버깅 여부 설정

    Date2021.03.29 Views498
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved