这篇文章主要为大家详细介绍了php json_decode()和json_encode()使用示例,具有一定的参考价值,可以用来参考一下。
对php中json_decode()和json_encode()的使用方法感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
/*** 来自四海网(www.q1010.com) ***/
上例将输出:
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
$data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]';
echo json_decode($data);
/*** 来自四海网(www.q1010.com) ***/
代码如下:
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
echo json_decode($data,true);
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
/*** 来自四海网(www.q1010.com) ***/
以上例程会输出:
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
{"a":1,"b":2,"c":3,"d":4,"e":5}
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
<?php
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");
echo "Normal: ", json_encode($a), "\n";
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ", json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
$b = array();
echo "Empty array output as array: ", json_encode($b), "\n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
$c = array(array(1,2,3));
echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
$d = array('foo' => 'bar', 'baz' => 'long');
echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
?>
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
<?php
echo "连续数组".PHP_EOL;
$sequential = array("foo", "bar", "baz", "blong");
var_dump(
$sequential,
json_encode($sequential)
);
echo PHP_EOL."非连续数组".PHP_EOL;
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
var_dump(
$nonsequential,
json_encode($nonsequential)
);
echo PHP_EOL."删除一个连续数组值的方式产生的非连续数组".PHP_EOL;
unset($sequential[1]);
var_dump(
$sequential,
json_encode($sequential)
);
?>
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
array(4) {
[1]=>
string(3) "foo"
[2]=>
string(3) "bar"
[3]=>
string(3) "baz"
[4]=>
string(5) "blong"
}
string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}"
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
array(3) {
[0]=>
string(3) "foo"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(33) "{"0":"foo","2":"baz","3":"blong"}"
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
$obj->Name= 'a1';$obj->Number ='123';
$obj->Contno= '000';
echo json_encode($obj);
/*** 来自四海网(www.q1010.com) ***/
/**
* php中json_decode()和json_encode()的使用方法
*
* @param
* @arrange 512-笔记网: q1010.com
**/
{"Name":"a1",
"Number":"123",
"Contno":"000"
}
/*** 来自四海网(www.q1010.com) ***/
本文来自:http://www.q1010.com/173/1329-0.html
注:关于php json_decode()和json_encode()使用示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:json_decode(),json_encode()
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。