메뉴 건너뛰기

프로그램언어

2014.03.26 02:12

JSON and JavaScript usage

조회 수 19087 추천 수 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

    Date2017.03.06 Views18542
    Read More
  2. 오류 메시지 출력(alert) 및 페이지 이동(refresh) 관련

    Date2017.03.06 Views18603
    Read More
  3. mysql_real_escape_string 이진 데이터를 입력할 경우 이 함수를 사용해야 함

    Date2016.12.23 Views18669
    Read More
  4. PHP 날짜/시간 정리

    Date2017.03.07 Views18672
    Read More
  5. 네이버 지도 API 연동 PHP 소스

    Date2017.03.06 Views18691
    Read More
  6. mysql_insert_id

    Date2016.12.23 Views18776
    Read More
  7. 문자열 치환 (str_replace)

    Date2016.12.23 Views18828
    Read More
  8. mysql_affected_rows — 최근 MySQL 작업으로 변경된 행 개수를 얻음

    Date2016.12.23 Views18828
    Read More
  9. 로그인페이지에서 온 경우/로그인한 페이지로 이동

    Date2016.12.23 Views18865
    Read More
  10. 문자열 추출하기 (substr)

    Date2016.12.23 Views18886
    Read More
  11. 두 날짜 사이의 차이 구하기

    Date2017.03.07 Views18907
    Read More
  12. 문자열 찾기 (strstr)

    Date2016.12.23 Views18927
    Read More
  13. 문자열 뒤집기 (strrev)

    Date2016.12.23 Views18961
    Read More
  14. 도메인 앞에 자동으로 WWW를 붙이는 방법

    Date2017.03.07 Views18963
    Read More
  15. mysql_result — 결과 데이터를 반환

    Date2016.12.23 Views18985
    Read More
  16. 정규 표현식 검색과 치환 (preg_replace)

    Date2016.12.23 Views19025
    Read More
  17. 디렉토리 안의 파일의 내용들을 읽는 예

    Date2016.12.23 Views19036
    Read More
  18. 링크를 걸때 http 처리방법

    Date2016.12.23 Views19040
    Read More
  19. 자릿수만큼 앞에 0 붙이기

    Date2017.03.07 Views19081
    Read More
  20. JSON and JavaScript usage

    Date2014.03.26 Views19087
    Read More
Board Pagination Prev 1 ... 6 7 8 9 10 11 12 13 14 15 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved