메뉴 건너뛰기

프로그램언어

2014.03.26 02:12

JSON and JavaScript usage

조회 수 19081 추천 수 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. 환경변수 HTTP_USER_AGENT를 이용해서 스마트 기기 분류하기

    Date2016.09.21 Views25974
    Read More
  2. 확장자 추출 하기

    Date2021.03.26 Views329
    Read More
  3. 홈페이지 귀퉁이에 붙이는 공지창

    Date2015.04.06 Views25446
    Read More
  4. 헤더이용 다운로드 받을시 바로열기부분 소스

    Date2018.07.24 Views7357
    Read More
  5. 해당하는 날짜가 그달의 몇주째인지 계산

    Date2014.02.27 Views26367
    Read More
  6. 함수이름을 변수로 사용하기, 매개변수 없는 함수에 매개변수 넣기

    Date2021.03.26 Views762
    Read More
  7. 한글줄바꾸기 또는 utf-8 wordwrap

    Date2014.04.12 Views26558
    Read More
  8. 한글자르기 substr

    Date2015.04.14 Views25233
    Read More
  9. 한글이 깨져서 나올 때 - iconv

    Date2018.08.29 Views3959
    Read More
  10. 필드값 저장

    Date2014.02.27 Views24297
    Read More
  11. 프레임 사이트에서 새로고침(F5) 할때 초기화면으로 이동하지 않음

    Date2019.01.08 Views1297
    Read More
  12. 폴더에 사진올려놓고 리스트자동으로 만들기

    Date2019.01.08 Views1314
    Read More
  13. 폴더 용량 체크

    Date2023.01.12 Views236
    Read More
  14. 포트체크 방법

    Date2019.01.16 Views1314
    Read More
  15. 페이지 로딩 시간 측정

    Date2014.02.27 Views26063
    Read More
  16. 파일을 변수에 담기(ob_start를 이용한 방법)

    Date2021.03.26 Views700
    Read More
  17. 파일업로드

    Date2017.02.19 Views19366
    Read More
  18. 파일시스템, 폼 파일업로드 관련 함수

    Date2017.03.27 Views21698
    Read More
  19. 파일 확장자 비교

    Date2016.12.23 Views21998
    Read More
  20. 파일 종류에 따른 아이콘표시하기 함수

    Date2019.01.16 Views1442
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved