Home > php教程 > php手册 > PHP解析JSON与XML程序

PHP解析JSON与XML程序

WBOY
Release: 2016-05-25 16:41:56
Original
1349 people have browsed it

与大多数流行的 Web 服务如 twitter 通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据的各种传送格式,包括JSON,XML 等等,实例代码如下:

$json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} '; 
$obj=json_decode($json_string); 
echo $obj->name; //prints foo 
echo $obj->interest[1]; //prints php
Copy after login

PHP解析 XML 数据,实例代码如下:

<?php
$xml_string="<?xml version=&#39;1.0&#39; 
<users> 
<user id=&#39;398&#39;> 
<name>Foo</name> 
<email>foo@bar.com</name> 
</user> 
<user id=&#39;867&#39;> 
<name>Foobar</name> 
<email>foobar@foo.com</name> 
</user> 
</users>"; 
 
//load the xml string using simplexml 
$xml = simplexml_load_string($xml_string); 
 
//loop through the each node of user 
foreach ($xml->user as $user) 
{
//access attribute 
echo $user[&#39;id&#39;], &#39; &#39;; 
//subnodes are accessed by -> operator 
echo $user->name, &#39; &#39;; 
echo $user->email, &#39;<br />&#39;; 
}
Copy after login


本文地址:

转载随意,但请附上文章地址:-)

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template