【问题标题】:How to pretty format json data that is returned from a curl request?如何漂亮地格式化从 curl 请求返回的 json 数据?
【发布时间】:2016-12-12 09:28:52
【问题描述】:

在我们网站的时代,我使用 php 从 url http://datos.labplc.mx/movilidad/vehiculos/436per.json 创建了一个 curl 请求。它正在工作,它正在回显以下数据,即使不必回显它。如果您需要知道,我们从 curl_init 开始并以 curl_close 结束。

{
  "consulta": {
    "placa": "430PER",
    "tenencias": {
      "placa": "430PER",
      "adeudos": "2011,2012,2013,2014,2015,2016",
      "tieneadeudos": "1"
    },
    "infracciones": [
      {
        "folio": "04102237388",
        "fecha": "2010-03-11",
        "situacion": "No pagada",
        "motivo": "POR ESTACIONARSE EN  LAS VÍAS PRIMARIAS",
        "fundamento": "Artículo: 12, Fracción: I, Parrafo: , Inciso: ",
        "sancion": "10 unidades de cuenta "
      },
      {
        "folio": "04092079661",
        "fecha": "2009-06-19",
        "situacion": "No pagada",
        "motivo": "POR CERRAR U OBSTRUIR LA CIRCULACIÓN EN LA VÍA PÚBLICA CON VEHÍCULOS, PLUMAS, REJAS O CUALQUIER OTRO OBJETO.",
        "fundamento": "Artículo: 14, Fracción: VII, Parrafo: , Inciso: ",
        "sancion": "10 unidades de cuenta "
      }
    ],
    "verificaciones": "error"
  }
}

我的问题:

如何在 HTML 表格中显示返回的数据,使其看起来干净易读?

【问题讨论】:

  • 必须尝试将这些数据转换为 html 你可以使用 Datatable.js
  • 你已经尝试了什么?
  • 不确定要尝试什么。我被提到的返回结果卡住了。

标签: javascript php curl


【解决方案1】:

您应该使用json_decode()。所以它将json返回到数组中,然后你可以使用数组并在你的html表中填充数据

【讨论】:

  • 我可以按照 json_decode() 说明进行操作,但之后我如何将其设置为表格。您能给我准确的说明吗?将不胜感激。谢谢
【解决方案2】:

我主要通过添加 "pre" 标签来做到这一点

echo "<pre>";
print_r($JSON);
echo "</pre>";

在 HTML 中编辑

$string = file_get_contents("http://datos.labplc.mx/movilidad/vehiculos/436per.json"); 
$json = json_decode($string, true);

$lines = $json['consulta']['infracciones'];

echo "<table>";
foreach($lines as $line)
{
    echo "<tr>";
    echo "<td>" . $line['folio'] . "</tr>";
    echo "<td>" . $line['fecha'] . "</tr>";
    echo "<td>" . $line['situacion'] . "</tr>";
    echo "<td>" . $line['motivo'] . "</tr>";
    echo "<td>" . $line['fundamento'] . "</tr>";
    echo "<td>" . $line['sancion'] . "</tr>";
    echo "</tr>";
}
echo "</table>";

【讨论】:

  • 是的,但是我如何让它打印一个返回数据看起来很漂亮的表格,而不是像代码一样。谢谢
  • $string = file_get_contents("http://datos.labplc.mx/movilidad/vehiculos/436per.json"); $lines = $json['consulta']['infracciones']; echo "&lt;table&gt;"; foreach($lines as $line) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $line['folio'] . "&lt;/tr&gt;"; echo "&lt;td&gt;" . $line['fecha'] . "&lt;/tr&gt;"; echo "&lt;td&gt;" . $line['situacion'] . "&lt;/tr&gt;"; echo "&lt;td&gt;" . $line['motivo'] . "&lt;/tr&gt;"; echo "&lt;td&gt;" . $line['fundamento'] . "&lt;/tr&gt;"; echo "&lt;td&gt;" . $line['sancion'] . "&lt;/tr&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
  • 2010-09-10
  • 2022-11-18
相关资源
最近更新 更多