Skip to content
Thomas Weinert edited this page Aug 1, 2014 · 2 revisions

Reading JSON

FluentDOM is able to load JSON and convert it into an wellformed XML.

Loading JSON and reading data:

$fd = FluentDOM($json, 'text/json');
echo $fd->find('/*/phoneNumbers/*[type="home"]/number')->text();

Output

212 555-1234

JSON Example (from Wikipedia)

{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ]
}

XML genereated by FluentDOM

<?xml version="1.0" encoding="UTF-8"?>
<json:json xmlns:json="urn:carica-json-dom.2013">
  <firstName>John</firstName>
  <lastName>Smith</lastName>
  <age json:type="number">25</age>
  <address>
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <state>NY</state>
    <postalCode json:type="number">10021</postalCode>
  </address>
  <phoneNumbers json:type="array">
    <_>
      <type>home</type>
      <number>212 555-1234</number>
    </_>
    <_>
      <type>fax</type>
      <number>646 555-4567</number>
    </_>
  </phoneNumbers>
</json:json>
Clone this wiki locally