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

WBOY
Lepaskan: 2016-07-29 08:53:26
asal
1169 orang telah melayarinya

第一步:得到一个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>
Salin selepas log masuk

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>
Salin selepas log masuk
').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教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan