phpjson数据解析
1. 如何在php中解析json
PHP里面有json_encode(数组)可以把数组给转换成JSON字符串,
而json_decode(JSON字符串,boolean)可以把JSON字符串转换成数组或者对象类型,第二个参数boolean默认为false表示对象类型,true表示解析为数组类型通过下表访问
2. PHP解析JSON数据,在线等!!!
先用json_decode把json解析成一个Object。接着用php提供的foreach循环,遍历得到key-value对,就能达到你想要的东西了
3. php获得api返回的json数据后,如何解析
首先你需要使用对方约定方式获取,然后考虑是否使用缓存,最后获取到数据后使用json_decode函数解析成数组格式,接下来就是自己的逻辑代码了。
4. php如何读取json中的数据
先使用json_decode()函数解析成数组,在用变量等于解析出来的数组
$test=json_decode("json数据");
输出 echo $test->key;
5. 如何在PHP中解析json
目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。
从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码。
一、json_encode()
该函数主要用来将数组和对象,转换为json格式。先看一个数组转换的例子:
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
结果为
{"a":1,"b":2,"c":3,"d":4,"e":5}
由于json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者null。当中文使用GB2312编码,或者外文使用ISO-8859-1编码的时候,这一点要特别注意。
二、索引数组和关联数组
PHP支持两种数组,一种是只保存"值"(value)的索引数组(indexed array),另一种是保存"名值对"(name/value)的关联数组(associative array)。
由于javascript不支持关联数组,所以json_encode()只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式。
比如,现在有一个索引数组
$arr = Array('one', 'two', 'three');
echo json_encode($arr);
结果为:
["one","two","three"]
三、类(class)的转换
下面是一个PHP的类: class Foo { const ERROR_CODE = '404'; public $public_ex = 'this is public'; private $private_ex = 'this is private!'; protected $protected_ex = 'this should be protected'; public function getErrorCode() { return self::ERROR_CODE; } }
现在,对这个类的实例进行json转换:
$foo = new Foo;
$foo_json = json_encode($foo);
echo $foo_json;
输出结果是
{"public_ex":"this is public"}
可以看到,除了公开变量(public),其他东西(常量、私有变量、方法等等)都遗失了。
6. 怎么用php解析这样的json数据
使用json_decode函数解码成数组,然后以数组形式访问多维数组中键DestinationTag对应的值即可。
7. PHP 如何解析 json
alert(send);
ajax=tel();
ajax.open("post", "setLine.class.php",true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 改成这样就可以了
ajax.send({
'lineA':JSON.stringify(send);
});