【问题标题】:Using json_encode formatting looks bad [duplicate]使用 json_encode 格式看起来很糟糕[重复]
【发布时间】:2014-12-17 03:17:46
【问题描述】:

我正在尝试使用 SimpleXML 从 MRSS 提要中解析出一些数据,然后使用该数据创建 JSON。

这是我正在尝试的,它确实输出 JSON,但格式看起来很糟糕,并且在每个元素之前给出所有 [{"0":",我确定我做错了什么:

<?php
session_start();
$html = "";
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces

for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos 
  $title = $xml->channel->item[$i]->video;
  $link = $xml->channel->item[$i]->link;
  $title = $xml->channel->item[$i]->title;
  $pubDate = $xml->channel->item[$i]->pubDate;
  $description = $xml->channel->item[$i]->description;
  $titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
  $m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$VideoFileURL = $m_attrs["url"];

$arr = array($title, $description, $VideoFileURL);

$json_string = json_encode($arr, 128);

echo json_encode($arr, 128);
}

?>

我都试过了

$json_string = json_encode($arr, 128)

$json_string = json_encode($arr, JSON_PRETTY_PRINT)

“128”的打印效果似乎比“JSON_PRETTY_PRINT”好

有什么想法吗?我确信这很明显,但我是 PHP 的新手。谢谢!

【问题讨论】:

  • JSON_PRETTY_PRINT 等于 128。也许你可以具体化你的风格观察。 json_encode() 循环中的每个元素可能不是你想要的。
  • @mario 感谢您的反馈,不太清楚您所说的“具体化您的风格观察”是什么意思——您能详细说明一下吗?谢谢!

标签: php json simplexml


【解决方案1】:

您需要将元素转换为字符串。如果您不将它们转换为字符串,它们就是 SimpleXMLElements。如果你这样做var_dump($title);,你会得到object(SimpleXMLElement)[6] public 0 => string 'Weekend Top 5: Talladega' (length=24),这就是为什么你在每个元素之前都得到[{"0":"

<?php
session_start();
$html = "";
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces

for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos 
  $title = (string)$xml->channel->item[$i]->video;
  $link = (string)$xml->channel->item[$i]->link;
  $title = (string)$xml->channel->item[$i]->title;
  $pubDate = (string)$xml->channel->item[$i]->pubDate;
  $description = (string)$xml->channel->item[$i]->description;
  $titleid = (string)$xml->channel->item[$i]->children($namespaces['bc'])->titleid;
  $m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
  $VideoFileURL = (string)$m_attrs["url"];

  $arr = array('title' => $title, 'description' => $description, 'VideoFileURL' => $VideoFileURL);
  $json_string = json_encode($arr, JSON_PRETTY_PRINT);

  echo json_encode($arr, JSON_PRETTY_PRINT);
}

?>

【讨论】:

  • 感谢您的反馈,非常感谢。我只是复制并粘贴了您上面的脚本版本,现在我得到:警告:json_encode() 期望参数 2 很长,字符串在第 19 行的 /home/stranger/www/ingestdev/LiveAPIUIMethod/JSONCreateScript_01.php 中给出警告:json_encode() 期望参数 2 很长,字符串在 /home/stranger/www/ingestdev/LiveAPIUIMethod/JSONCreateScript_01.php 第 21 行给出
  • 您可以将其更改为 128 并查看它对您的效果。您说 128 和 JSON_PRETTY_PRINT 之间存在差异,但如果您不需要它,则不应该或只是将其关闭。
  • JSON_PRETTY_PRINT 需要 PHP 5.4.0。看到字符串警告意味着该常量未定义,并被替换为包含常量名称的字符串。因此,您使用了错误的 PHP 版本。最有可能的问题不是漂亮的编码,而只是转换为字符串。在重复关闭旁边,请参阅Pretty-Printing JSON with PHP 以供现场参考,并记住 Stackoverflow 最适合一次问一个问题。还有the X/Y problem
猜你喜欢
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 2014-04-09
相关资源
最近更新 更多