메뉴 건너뛰기

프로그램언어

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. No Image 27Feb
    by
    2014/02/27 Views 26945 

    GD를 이용한 스팸성 게시물 차단을 위한 보안 단어 입력 예제

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

    glob 현재 디렉토리에서 pattern에 일치하는 경로 이름을 배열로 반환

  3. No Image 23Dec
    by
    2016/12/23 Views 19409 

    headers_sent 헤더 전송 여부를 확인

  4. No Image 26Mar
    by
    2021/03/26 Views 371 

    HEREDOC <<< ( PHP에서 echo로 HTML쉽게 표시하기 )

  5. No Image 12Apr
    by
    2014/04/12 Views 30443 

    htmlentities <-> html_entity_decode (엔티티

  6. No Image 22Apr
    by
    2016/04/22 Views 20637 

    HTTP Protocol의 data method - GET / POST

  7. HTTP 인증하기, 로그인창 띄우기

  8. No Image 07Mar
    by
    2017/03/07 Views 17642 

    http://홈주소/?mode=xxx 하는방법

  9. No Image 12Apr
    by
    2014/04/12 Views 21862 

    iframe 사용시 iframe의 높이가 내용의 높이만큼 자동으로 조절

  10. No Image 23Dec
    by
    2016/12/23 Views 19951 

    implode — 문자열로 배열 원소를 결합

  11. No Image 08Jan
    by
    2019/01/08 Views 1099 

    include 와 namespace

  12. No Image 23Dec
    by
    2016/12/23 Views 19520 

    ini_get - php.ini에 지정되어 있는 지시어의 값을 읽어온다

  13. No Image 19Feb
    by
    2019/02/19 Views 1217 

    input 자동완성기능 끄기

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

    is_array — 변수가 배열인지 확인

  15. Javascript 두 좌표 사이의 거리 구하기, 두 좌표의 중앙 좌표 구하기

  16. No Image 26Mar
    by
    2021/03/26 Views 1618 

    JAVASCRIPT 차트, 그래프 모음 (php chart, graph )

  17. No Image 26Mar
    by
    2014/03/26 Views 19522 

    jQuery 개발자를 위한 메모 - 레퍼런스

  18. No Image 26Mar
    by
    2014/03/26 Views 19257 

    jQuery 개발자를 위한 메모 - 플러그 인

  19. No Image 12Apr
    by
    2014/04/12 Views 20341 

    jquery 이용 아이디 중복체크 실시간

  20. No Image 26Mar
    by 조쉬
    2014/03/26 Views 19064 

    JSON and JavaScript usage

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

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved