【问题标题】:Json API -> Trying to get property of non-objectJson API -> 试图获取非对象的属性
【发布时间】:2015-05-06 12:43:14
【问题描述】:

我已经阅读了很多关于此的不同文章,但似乎找不到有效的解决方案。

我目前正在从 API 获取 json 数据(我必须先授权连接)并从中输出数据。 (只是点点滴滴,比如姓名、身份证等,不是全部)

首先,让我向您展示我的代码:

class Connector {
//authorization to the API
public $token;
private $username = 'myuser';
private $password = 'mypassword';

public function __construct() {
    $this->getToken();
    setcookie ('token', $this->token, time() + 3600, 'https://mycloud.com');
}

public function getToken() {
    $url = 'https://auth.mycloud.ch/sso2.php';

    $fields = array(
        'username' => $this->username,
        'password' => $this->password
    );
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );

    $result = curl_exec($ch);

    $this->token = json_decode($result, true)['token'];

    curl_close($ch);
}


public function getData($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_COOKIE, 'token=' . $this->token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch); 

    curl_close($ch);

    echo $result->{'id'};

    //print_r($result);


}
}


$cnt = new Connector();
$cnt->getData('https://hr.mycloud.ch/services/api/employee/10');

由于某种原因,当我执行 echo $result->{'id'}; 时它无法输出数据;
它告诉我“试图获取非对象的属性”
但我已经在上面使用了 json_decode...
$this->token = json_decode($result, true)['token'];

当我做 print_r($result);它给了我这个数组:

 [{"id":"10","name":"name","employee_id":"id@test.com","user_settings_id":"0","location_id":"1","company_id":"1","active":"1","employee_type":"type_ch"}] 


我到底做错了什么?我也尝试过使用 foreach 循环,但它给了我同样的错误。
希望你能帮我解决这个问题,谢谢。

【问题讨论】:

  • 编辑:我注意到我没有解码函数 getData 中的 $result,我只是试了一下,它仍然给我同样的错误
  • 查看json_decode()var_dump() 返回的内容。它可能是一个数组,而不是和对象。
  • 是的,它告诉我这是一个数组......
  • 不抱歉,我不小心输出了一个不同的变量。它返回一个数组
  • 那么@degr 的答案应该适用于此。

标签: php json object decode


【解决方案1】:

因为它是一个对象数组

$res = json_decode($result);

     foreach($res as $item){
        echo $item->{'id'};
     }

$res[0]->id;

【讨论】:

  • 这给了我以下错误:为 foreach() 提供的参数无效
  • 谢谢,现在可以了!不知道为什么它之前说这是一个无效的论点。
  • 而且,像往常一样,我使用 json_decode($result, true);在这种情况下,您的所有对象都将被解释为关联数组。
  • Don't know why it said it was an invalid argument before. 因为 foreach 期望成为一个数组作为第一个参数,你试图传递一个字符串 ;)
  • 当我应用 true 时,它​​再次给了我非对象错误:P
【解决方案2】:

你没有在 curl 返回的字符串上使用 json_decode 并且你没有得到一个对象,你得到一个对象数组,使用:

<?php
public function getData($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_COOKIE, 'token=' . $this->token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch);
    $res = json_decode($result);

    curl_close($ch);

    // sidenote: Always check, if json_decode was successful, it can return false on failure
    if ( $res !== null ) {
        // you get an array of objects, not an object, see your json input
        echo $result[0]->{'id'};
    }

    //print_r($result);
}

注意:您应该检查json_decode 是否返回null(null 是失败时的返回值)。

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 2021-09-02
    • 1970-01-01
    • 2014-06-04
    • 2022-07-01
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多