大家好:
我有一個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 />
我該怎麼做比較好,麻煩大家教我一下
謝謝
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"}
謝謝dragonH 大大,我當初也有用這個方法,但是我是寫
$json_data["_content"]
一直印不出來,結果一直換方法就越換越錯.....
感謝大大指點
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
了解,我再試試看
https://jsoneditoronline.org/ 線上json編輯,可以用來驗證你的json格式,我看你源始的json就有錯了…
混水摸魚
這json是我去特定網址抓的,我會在注意看看
謝謝你提供的json編輯器