2015-06-30 78 views
-1

在php中如何獲取activity_id_0和student_phone_0等的值。 我的JSON字符串就像php獲取json字符串鍵值

array(
    'json_text' => '{ 
    "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"}, 
    "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""} 
    }' 
) 
+0

你嘗試過什麼??? – Saty

+0

我試過json_decode,但是它給了我錯誤 – vnnogile

回答

0

可以作爲陣列此JSON解碼並通過按鍵得到它:

$a = array(
    'json_text' => '{ 
    "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"}, 
    "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""} 
    }' 
); 
$json = json_decode($a['json_text'],true); 
$activity_id_0 = $json["student_activity"]["activity_id_0"]; 
$student_phone_0 = $json["student_contact_info"]["student_phone_0"]; 
echo $activity_id_0,' ',$student_phone_0; //1 7867857986 
0

使用json_decode()

$arr = array(
    'json_text' => '{ 
     "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"}, 
     "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""} 
    }' 
); 

var_dump(json_decode($arr['json_text'])->student_contact_info->student_phone_0); 

的PHP手冊更多信息:http://php.net/manual/en/function.json-decode.php

0

這裏您需要將json ob JECT到一個數組,試試下面的代碼

$info = array(
     'json_text' => '{ 
     "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"}, 
     "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""} 
     }' 
    ); 

    // here we can convert json object into an array 
    $arrayInfo = json_decode($info['json_text'], true); 

    echo '<pre>'; 
    print_r($arrayInfo['student_activity']); 
    print_r($arrayInfo['student_contact_info']); 

我認爲它會爲你有用.. :-)