php解析JSON与XML数据的实现代码

原创
2016-07-25 08:58:49 754浏览
  1. $json_string='{"id":1,"name":"foo","email":"foo@jbxue.com","interest":["wordpress","php"]} ';
  2. $obj=json_decode($json_string);
  3. echo $obj->name; //prints foo
  4. echo $obj->interest[1]; //prints php
复制代码

2,PHP解析XML 数据

  1. $xml_string="

  2. Test
  3. test@jbxue.com
  4. News
  5. fnews@jbxue.net
  6. ";
  7. //load the xml string using simplexml

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

  10. foreach ($xml->user as $user)
  11. {
  12. //access attribute
  13. echo $user['id'], ' ';
  14. //subnodes are accessed by -> operator
  15. echo $user->name, ' ';
  16. echo $user->email, '
    ';
  17. }
复制代码

如今,json在数据传输上的应用越来越广了,建议大家好好掌握下。



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