当前位置:Gxlcms > PHP教程 > php解析JSON与XML的小例子

php解析JSON与XML的小例子

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

  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. ?>

人气教程排行