首页  >  文章  >  后端开发  >  php json接口转化为数组 生成xml接口 php 解析json php php json decod

php json接口转化为数组 生成xml接口 php 解析json php php json decod

WBOY
WBOY原创
2016-07-29 08:53:261178浏览

第一步:得到一个json接口,可通过一个url地址获得
第二步:通过file_get_contents取到该json串
第三步:将json格式的字符串(对象)转化为数组形式,
json_decode(json,true)json 待解码的json string字符串
当参数二为true 则结果返回的是array
第四步:foreach遍历数组成xml类型的接口

<code><span><?php </span><span>//header('content-type:application/json');</span><span>$url</span> = <span>'http://zphteach.com/zph/8wei/json/json.php'</span>;<span>//拿到url地址</span><span>$file</span> = file_get_contents(<span>$url</span>); <span>//采集到地址的内容</span><span>//echo $file;</span><span>$json</span> = json_decode(<span>$file</span>,<span>true</span>);
<span>//print_r($json) ;die;</span>
header(<span>'content-type:application/xml'</span>); <span>//xml头部</span><span>echo</span><span>'<?xml version="1.0" encoding="utf-8"?>'</span>;
<span>echo</span><span>'<root>'</root></span>; <span>//标签名对应使用</span><span>foreach</span> (<span>$json</span><span>as</span><span>$key</span> => <span>$value</span>) {
<span>echo</span><span>"<span_>.<span>$key</span>.<span>">"</span>;
<span>foreach</span> (<span>$value</span><span>as</span><span>$k</span> => <span>$v</span>) {
<span>echo</span><span>".<span>$k</span>.<span>">"</span>;
<span>$val</span> = str_replace(<span>"'"</span>, <span>"'"</span>, <span>$v</span>); <span>//字符串替换成xml能够解析的</span><span>$val</span> = str_replace(<span>", <span>"<"</span>, <span>$v</span>);
<span>echo</span><span>$val</span>;
<span>echo</span><span>""</span>.<span>$k</span>.<span>">"</span>;
}
<span>echo</span><span>"</span></span></span></span_>"</span>.<span>$key</span>.<span>">"</span>;
}
<span>echo</span><span>''</span>;</span></code>

url地址得到的内容如下:

<code>[{"<span>name</span>":<span><span>"zhangsan's"</span></span>,"<span>sex</span>":<span><span>"man"</span></span>},{"<span>name</span>":<span><span>"lisi<zhangsan></zhangsan></span>,"<span>sex</span>":<span><span>"woman"</span></span>}]</span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php json接口转化为数组 生成xml接口,包括了json,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn