当前位置:Gxlcms > PHP教程 > php解析JSON与XML数据的实现代码

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

时间:2021-07-01 10:21:17 帮助过:16人阅读

  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在数据传输上的应用越来越广了,建议大家好好掌握下。

人气教程排行