首页  >  文章  >  后端开发  >  php解析JSON与XML的小例子

php解析JSON与XML的小例子

WBOY
WBOY原创
2016-07-25 09:04:121144浏览
  1. $json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} ';
  2. $obj=json_decode($json_string);
  3. echo $obj->name; //prints foo
  4. echo $obj->interest[1]; //prints php
  5. ?>
复制代码

2、解析XML 数据

I)、xml文件

  1. $xml_string="
  2. Foo
  3. foo@bar.com
  4. Foobar
  5. foobar@foo.com
  6. ";
复制代码

II)、解析xml的代码

  1. //load the xml string using simplexml

  2. $xml = simplexml_load_string($xml_string);
  3. //loop through the each node of user

  4. foreach ($xml->user as $user)
  5. {
  6. //access attribute
  7. echo $user['id'], ' ';
  8. //subnodes are accessed by -> operator
  9. echo $user->name, ' ';
  10. echo $user->email, '
    ';
  11. }
  12. ?>
复制代码


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