메뉴 건너뛰기

프로그램언어

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 26Mar
    by 조쉬
    2014/03/26 Views 19064 

    JSON and JavaScript usage

  2. No Image 07Mar
    by
    2017/03/07 Views 19028 

    자릿수만큼 앞에 0 붙이기

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

    링크를 걸때 http 처리방법

  4. No Image 23Dec
    by
    2016/12/23 Views 19012 

    정규 표현식 검색과 치환 (preg_replace)

  5. No Image 23Dec
    by
    2016/12/23 Views 19012 

    디렉토리 안의 파일의 내용들을 읽는 예

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

    mysql_result — 결과 데이터를 반환

  7. No Image 23Dec
    by
    2016/12/23 Views 18950 

    문자열 뒤집기 (strrev)

  8. No Image 23Dec
    by
    2016/12/23 Views 18907 

    문자열 찾기 (strstr)

  9. No Image 07Mar
    by
    2017/03/07 Views 18885 

    도메인 앞에 자동으로 WWW를 붙이는 방법

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

    문자열 추출하기 (substr)

  11. No Image 07Mar
    by
    2017/03/07 Views 18866 

    두 날짜 사이의 차이 구하기

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

    로그인페이지에서 온 경우/로그인한 페이지로 이동

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

    문자열 치환 (str_replace)

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

    mysql_affected_rows — 최근 MySQL 작업으로 변경된 행 개수를 얻음

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

    mysql_insert_id

  16. No Image 23Dec
    by
    2016/12/23 Views 18640 

    mysql_real_escape_string 이진 데이터를 입력할 경우 이 함수를 사용해야 함

  17. No Image 06Mar
    by
    2017/03/06 Views 18635 

    네이버 지도 API 연동 PHP 소스

  18. No Image 07Mar
    by
    2017/03/07 Views 18629 

    PHP 날짜/시간 정리

  19. No Image 06Mar
    by
    2017/03/06 Views 18568 

    오류 메시지 출력(alert) 및 페이지 이동(refresh) 관련

  20. No Image 06Mar
    by
    2017/03/06 Views 18502 

    잡다한 php

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

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved