Home >Backend Development >PHP Tutorial >Convert php json interface into array Generate xml interface php parse json php php json decod

Convert php json interface into array Generate xml interface php parse json php php json decod

WBOY
WBOYOriginal
2016-07-29 08:53:261211browse

第一步:得到一个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>'</span>; <span>//标签名对应使用</span><span>foreach</span> (<span>$json</span><span>as</span><span>$key</span> => <span>$value</span>) {
<span>echo</span><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>.<span>$k</span>.<span>">"</span>;
<span>$val</span> = str_replace(<span>"'"</span>, <span>"&apos;"</span>, <span>$v</span>); <span>//字符串替换成xml能够解析的</span><span>$val</span> = str_replace(<span>"<"</span>, <span>"&lt;"</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>$key</span>.<span>">"</span>;
}
<span>echo</span><span>'</root>'</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"</span></span>,"<span>sex</span>":<span><span>"woman"</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教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn