【问题标题】:Variable not parsed JSON变量未解析 JSON
【发布时间】:2015-01-30 17:21:00
【问题描述】:

我正在尝试将 JSON 值从 Disqus 解析为 PHP 变量,我能够完成 90%,但由于某种原因,某个特定变量无法正常工作。

$comment->message 和 $comment->thread 在下面的 JSON 中都有值。

JSON:https://disqus.com/api/3.0/forums/listPosts.json?api_key=0B7l7oEVh6xH6EN5BEcEDg4R7tq4RiEmhuLyjnavaUKyOLx23bo099ltdnH9f2p6&forum=greetingtheworld&limit=4

<?php

$endpoint = 'https://disqus.com/api/3.0/forums/listPosts.json?api_key=0B7l7oEVh6xH6EN5BEcEDg4R7tq4RiEmhuLyjnavaUKyOLx23bo099ltdnH9f2p6&forum=greetingtheworld&limit=4';

$j=0;
$cursor=0;

// Standard CURL
$session = curl_init($endpoint.$cursor);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // instead of just returning true on success, return the result on success
$data = curl_exec($session);
curl_close($session);

// Decode JSON data
$results = json_decode($data);
if ($results === NULL) die('Error parsing json');

// Comment response
$comments = $results->response;

foreach ($comments as $comment) {
    $name = $comment->author->name; <-- THIS WORKS
    $comment = $comment->message; <-- THIS WORKS
    $thread = $comment->thread; <-- THIS DOESNT WORK
    // Get more data...
    echo '<li class="recentcomments">
        <span class="comment-author-link">';
    echo $name;
    echo '</span> on <a href="2013/10/take-a-deep-breath-and-just-be/index.html#comment-116">';
    echo $comment . $thread;
    echo "</a></li>";
}

?>

执行上述操作时,以下操作正常并返回正确的值:

$comment = $comment->message;

但是,以下返回错误:

$thread = $comment->thread;

注意:试图在第 27 行的 /home/... 中获取非对象的属性

非常感谢您的帮助!

【问题讨论】:

  • 我们看不到 JSON。

标签: php json disqus


【解决方案1】:

在上面的代码中,您将$comment-&gt;message 存储到$comment。所以$comment-&gt;thread 不起作用,因为您在 before 语句中更改了$comment 的值。 因此您必须将$comment-&gt;message 存储到任何其他变量,例如$message

【讨论】:

  • 这太明显了!非常感谢,伙计。有时我们只需要不同的眼睛来看待我们的代码。近乎尴尬!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多