I
I
Ivan2017-01-29 12:23:02
PHP
Ivan, 2017-01-29 12:23:02

How to convert json to html?

How to convert (Align) json data to html
There is a json file in which there are a bunch of lines how to convert (distribute) these lines as needed
Example
json code

{
  "meta": {
    "p": 273876598,
    "a": 370637443,
    "t": "Сообшение 1",
    "d": 1485679498
  },
  "data": [
    {
      "i": 56645,
      "f": 370637443,
      "t": "Сообшение 2",
      "d": 346934331
    },
    {
      "i": 56644,
      "f": 370637443,
      "t": "Сообшение 3",
      "d": 346934327
    }
}

Should work
1. Сообшение 1
19.01.17 12:23

2. Сообшение 4
19.01.17 12:27

3. Сообшение 3
19.01.17 12:26

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
catrinblaidd, 2017-01-29
@Nickname-no

Your json is not very clear. It's not clear where the date of the attached messages comes from. But what you need is done like this:

\date_default_timezone_set('Europe/Moscow');
$json = '{
  "meta": {
    "p": 273876598,
    "a": 370637443,
    "t": "Сообшение 1",
    "d": 1485679498
  },
  "data": [
    {
      "i": 56645,
      "f": 370637443,
      "t": "Сообшение 2",
      "d": 346934331
    },
    {
      "i": 56644,
      "f": 370637443,
      "t": "Сообшение 3",
      "d": 346934327
    }
  ]
}';
$arr = \json_decode($json, true);
$html = '';
foreach ($arr as $key => $val) {
  if (isset($val['t']) && isset($val['d'])) {
    $date = \date('d.m.y H:i', $val['d']);
    $html .= "<p>{$val['t']}<br>$date</p>";
  } else {
    foreach ($val as $key2 => $val2) {
      if (isset($val2['t']) && isset($val2['d'])) {
        $date = \date('d.m.y H:i', $val2['t']);
        $html .= "<p>{$val2['t']}<br>$date</p>";
      }
    }
  }
}
echo $html;

R
riot26, 2017-01-29
@riot26

php.net/manual/ru/function.file-get-contents.php
php.net/manual/ru/function.json-decode.php
php.net/manual/ru/control-structures.foreach.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question