Home  >  Article  >  Backend Development  >  Implementation code for php parsing JSON and XML data

Implementation code for php parsing JSON and XML data

WBOY
WBOYOriginal
2016-07-25 08:58:49996browse
  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在数据传输上的应用越来越广了,建议大家好好掌握下。



Statement:
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