【问题标题】:Attempting to grab data from json with php returns nothing尝试使用 php 从 json 中获取数据不返回任何内容
【发布时间】:2018-03-20 02:31:01
【问题描述】:

我目前在用 PHP 从我的 json 文件中获取数据时遇到问题。

我现在得到的是

$jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}', true);

echo ($jsondecoded[0]->chat);

我正在尝试获取聊天信息。但它没有回应任何东西。我一直在试图弄清楚为什么会这样,但很遗憾我找不到。

【问题讨论】:

  • 无效的 json,最后缺少 ]
  • 遗憾的是,这不是问题所在。我修复了它,同样的事情发生了。

标签: php json


【解决方案1】:

三个错误:

1) json 末尾缺少 ]。

2)您使用的是json_decode的“array assoc”选项,所以它没有返回一个对象。

3) 您不能回显聊天对象。

试试这个:

$jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}]');

echo ($jsondecoded[0]->chat->username);

【讨论】:

  • 4) echo 是一个构造,不需要用括号括起来。
  • 哇。不敢相信我会错过这么简单的事情。感谢 Dionei 的帮助,将来会非常有用。
【解决方案2】:
$jsondecoded = json_decode('[{"chat": {"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}]', true);

另外,请记住,您已将 true 用作 json_decode 的第二个参数,这意味着您要将结果转换为关联数组,要么删除 true,要么更改您的访问权限:

var_dump($jsondecoded[0]['chat']);

【讨论】:

    【解决方案3】:

    您的 json 在末尾无效,在 json 字符串的末尾添加 ']',并在 php 中使用 print_r 函数打印数组。 '[0]' 用于获取第一个元素,['chat'] 用于获取聊天键的值

    $jsondecoded = json_decode('[{"chat":{"username":"RobloxProtectorKing","message":":slender me","time":"2018-03-20 01:56:12"}}]', true);
    
    print_r ($jsondecoded[0]['chat']);
    

    输出会喜欢

    Array
    (
        [username] => RobloxProtectorKing
        [message] => :slender me
        [time] => 2018-03-20 01:56:12
    )
    

    【讨论】:

      【解决方案4】:

      这是我拥有的模板中的一个示例。希望对您有所帮助。

      <?php
      /* Status Codes
      
      return 0 = Nothing to Update (n/a)
      return 1 = Successful Insert Query
      return 2 = Database Connection refused
      return 3 = MySQL Query Error OR Wrong URL Parameters */
      
      /* Disable Warnings so that we can return ONLY what we want through echo. */
      mysqli_report(MYSQLI_REPORT_STRICT);
      
      // First get raw POST input
      $raw_post     = file_get_contents('php://input');
      // Run through url_decode..
      $url_decoded  = urldecode($raw_post);
      // Run through json_decode...
      // false to allow for reference to oject. eg. $column->name instead of $column["name"] in the foreach.
      $json_decoded = json_decode($url_decoded, false);
      
      $pk_line_item_id  = (strlen($json_decoded[0]->value)  > 0 ? $json_decoded[0]->value : null);
      
      // INCLUDE DB CONNECTION STRING
      include 'php_pdo_mysql_connect.php';
      
      // SQL INSERT query...
      $stmt = $link->prepare(" SELECT * FROM tbl_xyz ");
      
      //$stmt->bindParam(':pk_line_item_id', $pk_line_item_id, PDO::PARAM_INT);   // INT
      
      // Execute this SQL statement.
      $stmt->execute();
      
      // Fetch & Populate the single returned in var $resultSet.
      $resultSet = $stmt->fetchAll(); 
      // Returns an array indexed by column number as returned in your result set, starting at column 0. 
      // https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.swg.im.dbclient.php.doc/doc/t0023505.html
      
      // If the search record was found, populate it on the html table.
      if (($resultSet !== false) && ($stmt->rowCount() > 0)) {
      
      // Set JSON headers
      header('Content-type:application/json;charset=utf-8');
      
      // Encode entire $resultSet array to JSON.
      $json_encoded = json_encode($resultSet);
      // Ready to return as JSON to client side JQuery / Java Script...
      
      echo $json_encoded;
      
      }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-31
        • 2015-12-16
        • 1970-01-01
        • 2017-11-21
        • 1970-01-01
        • 2021-08-28
        • 1970-01-01
        相关资源
        最近更新 更多