aa = ["한글", "id", "contactId", "fullName"];껌임.
_params['test'] = Ext.util.JSON.encode( aa );
헌데 여기서 엄청난 사질이 있었음...
php쪽
$test = stripslashes( iconv('utf-8', 'cp949', $test ) );stripslahses안 해주면 decode안됨
print_r ( json_decode( $test ) )
aa = ["한글", "id", "contactId", "fullName"];껌임.
_params['test'] = Ext.util.JSON.encode( aa );
$test = stripslashes( iconv('utf-8', 'cp949', $test ) );stripslahses안 해주면 decode안됨
print_r ( json_decode( $test ) )
// view action이고 결과 출력이 XMLRPC일 경우 해당 모듈의 api method를 실행
if($this->module_info->module_type == 'view'){
if(Context::getResponseMethod() == 'XMLRPC' || Context::getResponseMethod() == 'JSON') {
$oAPI = getAPI($this->module_info->module, 'api');
if(method_exists($oAPI, $this->act)) {
$oAPI->{$this->act}($this);
}
}
}else if($this->module_info->module_type == 'controller'){
if(Context::getResponseMethod() == 'JSON'){
}
}
return true;
}
}
?>
# $str_json에는 json형식의 string 데이터가 들어간다.
# orderingConfirmList의 key에 array의 형식으로 데이터가 들어있음[{"ORDER_NO":"11"},{"ORDER_NO":"2"}] 모 이쯤 되겠지.
# 그라지~~~
my $_scalar = from_json( $_str_json );
my @values = @{$_scalar->{'orderingConfirmList'}};
foreach my $_v ( @values )
{
# print Dumper ( $_v );
print $_v->{ORD_NO}. "\n";
}
jsonData = Ext.util.JSON.decode("{'total_rows':4,'list':[{'trans_date_pos':'2008-4-4','domain':'test','warehouse':'K','qty':13},{'trans_date_pos':'2008-4-4','domain':'test','warehouse':'K','qty':13},{'trans_date_pos':'2008-4-4','domain':'test','warehouse':'K','qty':13},{'trans_date_pos':'2008-4-4','domain':'test','warehouse':'K','qty':13},{'trans_date_pos':'2008-4-5','domain':'test','warehouse':'K','qty':13},{'trans_date_pos':'2008-4-5','domain':'test','warehouse':'K','qty':13},{'trans_date_pos':'2008-4-5','domain':'test','warehouse':'K','qty':13}]}");생성된 json object를 store에 저장함
// ext.data.groupingstore는 해당 자료를 grouping하기 위해 store대신 사용함
store = new Ext.data.GroupingStore({
reader: new Ext.data.JsonReader({
totalProperty: 'total_rows',
root: "list",
fields: ["trans_date_pos","domain","warehouse","qty"]
}),
data: jsonData,
groupField:'trans_date_pos',
autoLoad: true
});
<script type="text/javascript">
var arr = new Array(3);
arr[0] = "Jani";
arr[1] = "Tove";
arr[2] = "Hege";
var arr2 = new Array(3);
arr2[0] = "John";
arr2[1] = "Andy";
arr2[2] = "Wendy";
document.write(arr.concat(arr2));
</script>
json에서 사용한다면
var objJson = new object();
objJson.list = [1,2,3,4];
라고 하면 아래와 같은 구조로 만들어 질 것 같다.
objJson = { list:[1,2,3] } 이라고 가정하고
objJson.list.concat( arr2 ) 정도이면 될까?
여러개의 언어를 동시에 사용하다 보니..헛갈리네. 나참 다들 비슷해서리..
참조 사이트: http://pear.php.net/pepr/pepr-proposal-show.php?id=198
<?php
if ( !function_exists('json_decode') ){
function json_decode($content, $assoc=false){
require_once 'Services/JSON.php';
if ( $assoc ){
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
$json = new Services_JSON;
}
return $json->decode($content);
}
}
if ( !function_exists('json_encode') ){
function json_encode($content){
require_once 'Services/JSON.php';
$json = new Services_JSON;
return $json->encode($content);
}
}
?>
먼저 json방식의 자료를 만들어야겠다.
array일 경우
var Beatles = ["Paul","John","George","Ringo"];
var Beatles = new Array("Paul","John","George","Ringo");
Object일 경우
var Beatles = {
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll"
}
아래의 경우는 위와 동일함
var Beatles = new Object();
Beatles.Country = "England";
Beatles.YearFormed = 1959;
Beatles.Style = "Rock'n'Roll";
Object 출력
alert(Beatles.Style); //Dot Notation
alert(Beatles["Style"]); //Bracket Notation
Array를 Object에 사용할 경우
var Beatles = {
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll",
"Members" : ["Paul","John","George","Ringo"]
}
Array내부에 Object사용
var Rockbands = [
{
"Name" : "Beatles",
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll",
"Members" : ["Paul","John","George","Ringo"]
},
{
"Name" : "Rolling Stones",
"Country" : "England",
"YearFormed" : 1962,
"Style" : "Rock'n'Roll",
"Members" : ["Mick","Keith","Charlie","Bill"]
}
]
Json Syntax
Javascript Object 와 흡사함
{
"Name" : "Beatles",
"Country" : "England",
"YearFormed" : 1959,
"Style" : "Rock'n'Roll",
"Members" : ["Paul","John","George","Ringo"]
}
내가 원하는것은 만들어진 Json을 어떻게 Ajax를 사용해 전송 할 것인가? 하는 것이다.
그 전에 Json Parser란게 필요하다.
사실 eval( ) 을 사용해서 parsing을 하고 있었는데 parser를 쓰면 더 좋단다. 흠흠..
http://www.json.org/json.js
순서는 다음과 같다.
Client Side
1. Json을 만든다.
2. Json파서를 사용해 stringify() 작업을 한다. 이건 object를 string으로 만들어 버리겠다는것?
3. Send the URL-encoded JSON string to the server as part of the HTTP Request
Sample:
var objJSON = {
"msg" : MSG
};
var strJSON = encodeURIComponent(JSON.stringify(objJSON));
new Ajax.Request("ReceiveJSON.jsp",
{
method: "post",
parameters: "strJSON=" + strJSON,
onComplete: Respond
});
Server Side(php)
strJSON 파라미터를 받아서 json_decode() 하게 되면 array의 형식으로 변경됨.
알아서 작업하면 됨....
-------------------------
헌데 분명
Content_Type => 'application/json',
Content => $json_req,
의 방법을 이용한 경우가 있는데 이럴때는 서버측에서 어떻게 해야 하나?
* configure시 php-config가 없다고 하는 경우가 있다.
./configure --with-php-config=/usr/local/php/bin/php-config
extension=json.so 를 php.ini / php.d에 설정해주라고 하는데 어떻게 하는거지?
간단한 예제 작성
<?
$val = array("abc" => 12,
"foo" => "bar",
"bool0" => false,
"bool1" => true,
"arr" => array(1, 2, 3, null, 5),
"float" => 1.2345
);
$output = json_encode($val);
echo $output."\n";
?>
~
작동 안됨
Unable to load dynamic library './json.so'
json.so 를 /usr/local/lib 로 가져다 넣음
php.ini에
; Directory in which the loadable extensions (modules) reside.
extension_dir = "./"
extension_dir = "/usr/local/php/lib/php/extensions"
extension=json.so 를 셋팅 해 줌
오에~
dk
결과:
php json.php
{"abc":12,"foo":"bar","bool0":false,"bool1":true,"arr":[1,2,3,null,5],"float":1.2345}
// 데이터 공유를 위한 간단한 표현방식 정도라고 얘기 할 수 있다.
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
// Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].method // "newURI"
// JSON text를 object롤 변경하기 위해서는 eval() function을 사용해야 한다.
// 보통 ajax에서 json으로 return받으면 eval을 사용해 object에 입력해야 함
var myObject = eval('(' + myJSONtext + ')');