iT邦幫忙

1

php 抓json 部分的值

  • 分享至 

  • xImage

大家好:
我有一個json的檔案,我想抓他 "_content": 的值
但都一直抓不到,以下是我的json和程式碼

{
"success": true,
"content": {},
"httpResponse": {
"status": 200,
"headers": {
"contentType": "application/json",
},
"_content": "{\"id\":\"611b03c0-1463-42d0-a1c4-f43e1b308c3e\",\"actor\":{\"mbox\":\"mailto:info@tincanapi.com\",\"objectType\":\"Agent\"},\"verb\":{\"id\":\"http://adlnet.gov/expapi/verbs/experienced\"},\"timestamp\":\"2020-07-15T07:14:47.881Z\",\"stored\":\"2020-07-15T07:14:47.881Z\",\"authority\":{\"objectType\":\"Agent\",\"account\":{\"homePage\":\"http://cloud.scorm.com\",\"name\":\"6FHzChshWrTfDVs-z7s\"},\"name\":\"admin\"},\"version\":\"1.0.0\",\"object\":{\"id\":\"http://rusticisoftware.github.com/TinCanPHP\",\"objectType\":\"Activity\"}},{\"id\":\"a8d05e55-4204-4070-b16f-e1ad7c49ee50\",\"actor\":{\"objectType\":\"Agent\",\"account\":{\"homePage\":\"http://localhost/moodle\",\"name\":\"2\"},\"name\":\"rrr\"}
"_metadata": {
"crypto": {
"protocol": "TLSv1.2",
},
"timed_out": false,
"blocked": true,
"eof": false,
"wrapper_data": [
"HTTP/1.1 200 OK",
],
"wrapper_type": "http",
}
}
}

程式碼:

<?php

$loader = require '../vendor/autoload.php';

$lrs = new TinCan\RemoteLRS(
    'http://......',
    '1.0.0',
    'account',
    'pwd'
);
$response = $lrs->queryStatements(['limit' =>3]);
$test2 = json_encode($response, JSON_PRETTY_PRINT);
HEADER('Content-Type: application/json; charset=utf-8');
$json = json_decode($test2,true);
echo $json ;
 foreach($json as $key=>$value)
{
    echo $key . "=>" . $value ;
}


?>

他卻只能顯示

success=>1<br />// 只把success顯示出來.....其他都不顯示

<b>Notice</b>:  Array to string conversion in <b>C:\xampp\htdocs\TinCanPHP\sample\index.php</b> on line <b>19</b><br />

我該怎麼做比較好,麻煩大家教我一下
謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
dragonH
iT邦超人 5 級 ‧ 2020-07-27 15:14:29
最佳解答

data.json

{
    "success": true,
    "content": {},
    "httpResponse": {
        "status": 200,
        "headers": {
            "contentType": "application/json"
        },
        "_content": "{\"id\":\"611b03c0-1463-42d0-a1c4-f43e1b308c3e\",\"actor\":{\"mbox\":\"mailto:info@tincanapi.com\",\"objectType\":\"Agent\"},\"verb\":{\"id\":\"http://adlnet.gov/expapi/verbs/experienced\"},\"timestamp\":\"2020-07-15T07:14:47.881Z\",\"stored\":\"2020-07-15T07:14:47.881Z\",\"authority\":{\"objectType\":\"Agent\",\"account\":{\"homePage\":\"http://cloud.scorm.com\",\"name\":\"6FHzChshWrTfDVs-z7s\"},\"name\":\"admin\"},\"version\":\"1.0.0\",\"object\":{\"id\":\"http://rusticisoftware.github.com/TinCanPHP\",\"objectType\":\"Activity\"}},{\"id\":\"a8d05e55-4204-4070-b16f-e1ad7c49ee50\",\"actor\":{\"objectType\":\"Agent\",\"account\":{\"homePage\":\"http://localhost/moodle\",\"name\":\"2\"},\"name\":\"rrr\"}",
        "_metadata": {
            "crypto": {
                "protocol": "TLSv1.2"
            },
            "timed_out": false,
            "blocked": true,
            "eof": false,
            "wrapper_data": [
                "HTTP/1.1 200 OK"
            ],
            "wrapper_type": "http"
        }
    }
}

code

<?php
    $string = file_get_contents('data.json');
    // print_r($string);
    $json_data = json_decode($string, true);
    // print_r($json_data);
    print_r($json_data["httpResponse"]["_content"]);

result

{"id":"611b03c0-1463-42d0-a1c4-f43e1b308c3e","actor":{"mbox":"mailto:info@tincanapi.com","objectType":"Agent"},"verb":{"id":"http://adlnet.gov/expapi/verbs/experienced"},"timestamp":"2020-07-15T07:14:47.881Z","stored":"2020-07-15T07:14:47.881Z","authority":{"objectType":"Agent","account":{"homePage":"http://cloud.scorm.com","name":"6FHzChshWrTfDVs-z7s"},"name":"admin"},"version":"1.0.0","object":{"id":"http://rusticisoftware.github.com/TinCanPHP","objectType":"Activity"}},{"id":"a8d05e55-4204-4070-b16f-e1ad7c49ee50","actor":{"objectType":"Agent","account":{"homePage":"http://localhost/moodle","name":"2"},"name":"rrr"}
看更多先前的回應...收起先前的回應...
joker iT邦新手 5 級 ‧ 2020-07-27 15:20:59 檢舉

謝謝dragonH 大大,我當初也有用這個方法,但是我是寫

$json_data["_content"]

一直印不出來,結果一直換方法就越換越錯.....
感謝大大指點

dragonH iT邦超人 5 級 ‧ 2020-07-27 15:25:44 檢舉

/images/emoticon/emoticon12.gif

joker iT邦新手 5 級 ‧ 2020-07-27 15:49:33 檢舉

dragonH
大大好:
不好意思,我試著想舉一反三一下

{
"statements": [
{
"id": "0048540a-5ceb-40a5-b801-30286e175693",
"actor": {
"mbox": "mailto:info@tincanapi.com",
"objectType": "Agent"
},
"verb": {
"id": "http://www.XXX"
},
"timestamp": "2020-07-15T07:15:02.007Z",
"stored": "2020-07-15T07:15:02.007Z",
"authority": {
"objectType": "Agent",
"account": {
 
"name": "ssss-z7s"
},
"name": "admin"
},
"version": "1.0.0",
"object": {
 
"objectType": "Activity"
}
},......

看到大大打

  print_r($json_data["httpResponse"]["_content"]);

我就覺得我抓"timestamp"得值,應該也能如法炮製
所以我就打這樣

  print_r($json_data["httpResponse"]["_content"]["statements"]["timestamp"]);

但為何這樣就會不行呢?我有按照順序打了耶??
跟大大請教下
謝謝

dragonH iT邦超人 5 級 ‧ 2020-07-27 16:06:00 檢舉

star1530

你的 statements 看起來是個 array

需要給他 index 才能取值

e.g.

statements[0]["timestamp"]

沒測試過

不過應該就這樣

joker iT邦新手 5 級 ‧ 2020-07-27 16:22:55 檢舉

dragonH
了解,我再試試看

ch_lute iT邦新手 5 級 ‧ 2020-07-27 16:44:58 檢舉

star1530我覺得問題只是在你沒把這個jasn看清楚,可以找個排列的工具看一下就好

joker iT邦新手 5 級 ‧ 2020-07-27 16:55:33 檢舉

ch_lutedragonH你好:
我已經完成了!!謝謝大家

$test4 ["statements"][0]["id"]

這樣就OK了

dragonH iT邦超人 5 級 ‧ 2020-07-27 17:01:49 檢舉

/images/emoticon/emoticon42.gif

0
混水摸魚
iT邦研究生 2 級 ‧ 2020-07-27 17:01:53

https://jsoneditoronline.org/ 線上json編輯,可以用來驗證你的json格式,我看你源始的json就有錯了…

joker iT邦新手 5 級 ‧ 2020-07-28 08:24:40 檢舉

混水摸魚
這json是我去特定網址抓的,我會在注意看看
謝謝你提供的json編輯器

我要發表回答

立即登入回答