【问题标题】:JSON get data with different namesJSON 获取不同名称的数据
【发布时间】:2016-10-19 06:36:49
【问题描述】:

我无法遍历这个 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"
}

}

【问题讨论】:

  • json_decode() 比循环

标签: php json loops object


【解决方案1】:

你需要在这里使用json_decode()从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
   }
}
?>

请注意,当您使用第二个参数为 TRUE 时,这将以数组格式返回结果,如果您想以对象格式获取结果,则可以忽略第二个参数,请阅读手册:json_decode()

如果您只想获取general_release 数据,则可以这样使用:

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

【讨论】:

  • 感谢您的回复。我已经在打印所有数据时使用了此代码。但我现在的问题是我怎样才能获得例如“general_release”?
  • @Kiko: 回答更新的 plz chk
  • @Kiko:你检查了吗?
  • 是的。谢谢你的时间。
  • 复制。再次感谢
【解决方案2】:
$data = json_decode($json);
$temp = $data->general_release;
var_dump($temp->type_of_form);

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

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多