php获取json字符串键和值

时间:2015-06-30 06:21:59

标签: php json

在php中如何获取activity_id_0和student_phone_0等的值。 我的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":""}
    }'
)

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 

答案 2 :(得分:0)

在这里你需要将json对象转换为数组,尝试下面的代码

$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']);

我认为这对你有帮助..: - )