欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-04-04 14:18:53
...
  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在数据传输上的应用越来越广了,建议大家好好掌握下。