Php JSON使用不同的名称获取数据

Php JSON使用不同的名称获取数据,php,json,loops,object,Php,Json,Loops,Object,我很难遍历这个JSON,因为它有不同的名称。如何循环并获取“一般发布”或“特殊移民少年基金”中的数据?我对json非常陌生,我认为这类事情对我来说很难处理 这是json,请忽略空值: { "referral": { "PD_Subject": "Urgent Please Respond", "PD_Location": "1", "referral_start_date": "10/19/2016 9:12 AM", "referral_end_date": "

我很难遍历这个JSON,因为它有不同的名称。如何循环并获取“一般发布”或“特殊移民少年基金”中的数据?我对json非常陌生,我认为这类事情对我来说很难处理

这是json,请忽略空值:

{
"referral": {
    "PD_Subject": "Urgent Please Respond",
    "PD_Location": "1",
    "referral_start_date": "10/19/2016 9:12 AM",
    "referral_end_date": "10/31/2016 9:12 AM",
    "publicdefender": "Rommel",
    "email_pd": "Rommel@yahoo.com",
    "number_pd": "123456",
    "socialworker": "Rommel",
    "email_sw": "rommel@yahoo.com",
    "number_sw": "12345567",
    "PB_firstname": "Rommel",
    "PB_lastname": "Semira",
    "PB_mobile": "12345678",
    "alternate_mobile": "1234578",
    "other_names": "Rommel",
    "PB_email_address": "staypot@rocketmail.com",
    "PB_address": "123213123",
    "PB_birthdate": "10/05/2016 9:12 AM",
    "PB_gender": "Male",
    "PB_birthplace": "Australia\r\n",
    "language": "English",
    "other_language": "Spanish",
    "guardian_fullname": "Rommel",
    "PB_relationshipToClient": "1234568",
    "guardian_address": "qwertyu",
    "guardian_number": "123123123",
    "guardian_alternatenumber": "123123123",
    "juvenile_dateOfArrest": "10/27/2016",
    "juvenile_charge": "123123123",
    "juvenile_disposition": "qweqweqwe",
    "PB_juvenile_courtDate": "10/22/2016",
    "court_date_location": "qweqweq",
    "PB_history_dateOfArrest": "10/29/2016",
    "history_charge": "qweqwe",
    "history_county": "Alpine",
    "registed_sss": "1",
    "history_outcome": "qweqweqwe",
    "referral_notes": "qweqweqwe"
},
"general_release": {
    "sijs_firm_name": "",
    "sijs_street_address": "",
    "sijs_city": "",
    "sijs_state": "",
    "sijs_zipcode": "",
    "sijs_telephone_number": "",
    "sijs_fax": "",
    "sijs_email_address": "",
    "sijs_attorney": "",
    "sijs_county": "",
    "sijs_mailing_address": "",
    "sijs_branch_name": "",
    "sijs_case_name": "",
    "sijs_case_number": "",
    "sijs_child_name": "",
    "sijs_date_of_birth": "",
    "sijs1_datetime_hearing": "",
    "sijs_department": "",
    "sijs_room": "",
    "sijs_judicial_officer": "",
    "sijs_attorney_presents": "",
    "sijs_child_country": "",
    "sijs_child_juvenile_court": "",
    "sijs_placed_under_custody_name": "",
    "sijs_custody_entity_name": "",
    "sijs_state_of_agency_location": "",
    "sijs_california_court_date": "",
    "general_appointment_id": "276",
    "type_of_form": "general_release"
},
"special_immigrant_juvenile_fundings": {
    "_token": " ti9qqsqzOMRzEo4PZd1I2wwWFSdyIM3jrXJTAenK",
    "sijs_firm_name": "",
    "sijs_street_address": "",
    "sijs_city": "",
    "sijs_state": "",
    "sijs_zipcode": "",
    "sijs_telephone_number": "",
    "sijs_fax": "",
    "sijs_email_address": "",
    "sijs_attorney": "",
    "sijs_county": "",
    "sijs_mailing_address": "",
    "sijs_branch_name": "",
    "sijs_case_name": "",
    "sijs_case_number": "",
    "sijs_child_name": "",
    "sijs_date_of_birth": "",
    "sijs1_datetime_hearing": "",
    "sijs_department": "",
    "sijs_room": "",
    "sijs_judicial_officer": "",
    "sijs_attorney_presents": "",
    "sijs_child_country": "",
    "sijs_child_juvenile_court": "",
    "sijs_placed_under_custody_name": "",
    "sijs_custody_entity_name": "",
    "sijs_state_of_agency_location": "",
    "sijs_california_court_date": "",
    "general_appointment_id": "276",
    "type_of_form": "special_immigrant_juvenile_fundings"
}
}

您需要使用此处从json字符串获取值,如下所示:

<?php
$json = '{
"referral": {
    "PD_Subject": "Urgent Please Respond",
    "PD_Location": "1",
    "referral_start_date": "10/19/2016 9:12 AM",
    "referral_end_date": "10/31/2016 9:12 AM",
    "publicdefender": "Rommel",
    "email_pd": "Rommel@yahoo.com",
    "number_pd": "123456",
    "socialworker": "Rommel",
    "email_sw": "rommel@yahoo.com",
    "number_sw": "12345567",
    "PB_firstname": "Rommel",
    "PB_lastname": "Semira",
    "PB_mobile": "12345678",
    "alternate_mobile": "1234578",
    "other_names": "Rommel",
    "PB_email_address": "staypot@rocketmail.com",
    "PB_address": "123213123",
    "PB_birthdate": "10/05/2016 9:12 AM",
    "PB_gender": "Male",
    "PB_birthplace": "Australia\r\n",
    "language": "English",
    "other_language": "Spanish",
    "guardian_fullname": "Rommel",
    "PB_relationshipToClient": "1234568",
    "guardian_address": "qwertyu",
    "guardian_number": "123123123",
    "guardian_alternatenumber": "123123123",
    "juvenile_dateOfArrest": "10/27/2016",
    "juvenile_charge": "123123123",
    "juvenile_disposition": "qweqweqwe",
    "PB_juvenile_courtDate": "10/22/2016",
    "court_date_location": "qweqweq",
    "PB_history_dateOfArrest": "10/29/2016",
    "history_charge": "qweqwe",
    "history_county": "Alpine",
    "registed_sss": "1",
    "history_outcome": "qweqweqwe",
    "referral_notes": "qweqweqwe"
},
"general_release": {
    "sijs_firm_name": "",
    "sijs_street_address": "",
    "sijs_city": "",
    "sijs_state": "",
    "sijs_zipcode": "",
    "sijs_telephone_number": "",
    "sijs_fax": "",
    "sijs_email_address": "",
    "sijs_attorney": "",
    "sijs_county": "",
    "sijs_mailing_address": "",
    "sijs_branch_name": "",
    "sijs_case_name": "",
    "sijs_case_number": "",
    "sijs_child_name": "",
    "sijs_date_of_birth": "",
    "sijs1_datetime_hearing": "",
    "sijs_department": "",
    "sijs_room": "",
    "sijs_judicial_officer": "",
    "sijs_attorney_presents": "",
    "sijs_child_country": "",
    "sijs_child_juvenile_court": "",
    "sijs_placed_under_custody_name": "",
    "sijs_custody_entity_name": "",
    "sijs_state_of_agency_location": "",
    "sijs_california_court_date": "",
    "general_appointment_id": "276",
    "type_of_form": "general_release"
},
"special_immigrant_juvenile_fundings": {
    "_token": " ti9qqsqzOMRzEo4PZd1I2wwWFSdyIM3jrXJTAenK",
    "sijs_firm_name": "",
    "sijs_street_address": "",
    "sijs_city": "",
    "sijs_state": "",
    "sijs_zipcode": "",
    "sijs_telephone_number": "",
    "sijs_fax": "",
    "sijs_email_address": "",
    "sijs_attorney": "",
    "sijs_county": "",
    "sijs_mailing_address": "",
    "sijs_branch_name": "",
    "sijs_case_name": "",
    "sijs_case_number": "",
    "sijs_child_name": "",
    "sijs_date_of_birth": "",
    "sijs1_datetime_hearing": "",
    "sijs_department": "",
    "sijs_room": "",
    "sijs_judicial_officer": "",
    "sijs_attorney_presents": "",
    "sijs_child_country": "",
    "sijs_child_juvenile_court": "",
    "sijs_placed_under_custody_name": "",
    "sijs_custody_entity_name": "",
    "sijs_state_of_agency_location": "",
    "sijs_california_court_date": "",
    "general_appointment_id": "276",
    "type_of_form": "special_immigrant_juvenile_fundings"
}
}';

$decode = json_decode($json,true); // use second param as true will return the result in array.
foreach ($decode as $key => $value) {
   echo $key."<br/>"; // this will print the heading key
   echo "Values:<br/>";
   foreach ($value as $finalValue) {
      echo $finalValue."<br/>"; // this will print the all values inside the sub array
   }
}
?>

如果我想将代码存储在变量中,这段代码也可以使用。

json\u decode()谢谢您的回复。我已经在打印所有数据时使用了此代码。但我现在的问题是,我如何才能只获得“通用发布”之类的内容?@Kiko:回答udpated plzchk@Kiko你查过了吗。谢谢你抽出时间。收到。再次感谢
foreach ($decode as $key => $value) {
  if($key == 'general_release'){
    echo "<pre>";
    print_r($value);
  }
}
$data = json_decode($json);
$temp = $data->general_release;
var_dump($temp->type_of_form);