JSON获取具有不同名称的数据

时间:2016-10-19 06:36:50

标签: php json loops object

我在浏览这个JSON时遇到了麻烦,因为它有不同的名称。如何在“general_release”或“special_immigrant_juvenile_fundings”中循环并获取数据?我在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"
}

}

2 个答案:

答案 0 :(得分:1)

您需要在此处使用json_decode()来获取json string中的值:

<?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
   }
}
?>

请注意,当您使用第二个参数作为TRUE时,这将以数组格式返回结果,如果您想获得对象格式的结果而不是忽略第二个参数,请阅读手册:{{3} }

如果你只想获得general_release数据,那么你可以使用这样的数据:

foreach ($decode as $key => $value) {
  if($key == 'general_release'){
    echo "<pre>";
    print_r($value);
  }
}

答案 1 :(得分:0)

$data = json_decode($json);
$temp = $data->general_release;
var_dump($temp->type_of_form);

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