2016-11-23 36 views
-4

我JSON數據:JSON解碼只有一個數據PHP

[ 
    { 
    "title": "red", 
    }, 
    { 
    "title": "blue", 
    }, 
    { 
    "title": "yellow", 
    }, 

] 

我想第一個數據,紅色。 我嘗試用這個

... 

    $json_output=curl_exec($ch); 
    $mydata = json_decode($json_output); 

    $result = $mydata->title; 

    echo $result[1]; 

$result = $mydata->title[1]; 

,但不工作。

我如何才能從此json獲取第一個「標題」數據?

+1

'var_dump($ mydata)'你看到了什麼? –

+1

'json_decode($ json_output,true)' - >'$ mydata [1] ['title'] // blue' – Xorifelse

+0

@Xorifelse它的索引爲'0'。 OP錯誤地使用索引'1'來獲取明顯錯誤的第一個條目 – rbr94

回答

1
$json_output=curl_exec($ch); 
$mydata = json_decode($json_output); 

$result = $mydata[0]->title; 
echo $result; 
+1

對這篇文章的任何解釋? – rbr94

+1

$ mydata是一個對象數組。所以你需要訪問它的索引$ mydata [0]。 –

+0

還添加了當前JSON字符串不是有效格式的事實。'當它失敗 – Xorifelse

1

首先你的JSON無效。您可以使用this驗證程序來檢查您的JSON是否有效。它應該看起來像下面這樣:

[ 
    { 
    "title": "red" 
    }, 
    { 
    "title": "blue" 
    }, 
    { 
    "title": "yellow" 
    } 

] 

有訪問JSON對象的方法有兩種:對象的

  1. 陣:

    $mydata = json_decode($json_output); 
    $title = $mydata[0]->title; // red 
    
  2. 關聯數組:

    $mydata = json_decode($json_output, true); 
    $title = $mydata[0]['title']; // red 
    

有關更多信息,請參閱json_decode()

+0

返回null'基於我從這個輸出你出[ { 「稱號」: 「紅色」, }我的回答, { 「稱號」: 「藍」, },{ 「title」:「yellow」, }, ] –

+1

@GianTomakin我不認爲此評論屬於此答案xD – rbr94

+0

您不必將其轉換爲數組。 '$ title = $ mydata [0] - > title;'也一樣。 –

1

根據PHP的手冊,json_decode以適當的PHP類型返回以JSON編碼的值。值true,false和null分別作爲TRUE,FALSE和NULL返回。如果JSON無法解碼或編碼數據比遞歸限制更深,則返回NULL。

<?php 
    $json_output = '[{ "title": "red" }, { "title": "blue" }, { "title": "yellow" }]'; 
    $mydata = json_decode($json_output); 
    var_dump($mydata); 
    /* Output: 
    array(3) { 
     [0]=> 
     object(stdClass)#1 (1) { 
     ["title"]=> 
     string(3) "red" 
     } 
     [1]=> 
     object(stdClass)#2 (1) { 
     ["title"]=> 
     string(4) "blue" 
     } 
     [2]=> 
     object(stdClass)#3 (1) { 
     ["title"]=> 
     string(6) "yellow" 
     } 
    } 
    */ 
    echo $mydata[0]->title; 
    // Output: red 
?> 

當第二個參數爲TRUE時,返回的對象將被轉換爲關聯數組。

<?php 
    $json_output = '[{ "title": "red" }, { "title": "blue" }, { "title": "yellow" }]'; 
    $mydata = json_decode($json_output, TRUE); 
    var_dump($mydata); 
    /* Ouput: 
    array(3) { 
     [0]=> 
     array(1) { 
     ["title"]=> 
     string(3) "red" 
     } 
     [1]=> 
     array(1) { 
     ["title"]=> 
     string(4) "blue" 
     } 
     [2]=> 
     array(1) { 
     ["title"]=> 
     string(6) "yellow" 
     } 
    } 
    */ 
    echo $mydata[0]['title']; 
    // Output: red 
?> 

在一個側面說明,訪問包含下PHP的命名約定不允許任何人物的對象內的元件可以通過用大括號包裹所述索引來完成。

<?php 
    $json_output = '[{ "h1-title": "red" }, { "h1-title": "blue" }, { "h1-title": "yellow" }]'; 
    $mydata = json_decode($json_output); 
    var_dump($mydata); 
    /* Output: 
    array(3) { 
     [0]=> 
     object(stdClass)#1 (1) { 
     ["h1-title"]=> 
     string(3) "red" 
     } 
     [1]=> 
     object(stdClass)#2 (1) { 
     ["h1-title"]=> 
     string(4) "blue" 
     } 
     [2]=> 
     object(stdClass)#3 (1) { 
     ["h1-title"]=> 
     string(6) "yellow" 
     } 
    } 
    */ 
    echo $mydata[0]->{'h1-title'}; 
    // Output: red 
?> 
+0

我個人不會花太多時間在*關閉*答案,特別是當它的排名較低的用戶。他們似乎忘記了他們問這個問題,甚至不標記或加1,但嘿所有'+ 1'都是爲了我的努力。 – Xorifelse