如何使用php引用具有键值对的json数组

如何使用php引用具有键值对的json数组,php,json,multidimensional-array,keyvaluepair,Php,Json,Multidimensional Array,Keyvaluepair,我已经阅读了类似的文章,但在我的案例中仍然需要一些帮助…我在从包含键和值对的json数组中获取值时遇到了麻烦: {"address": "4 Ficticious Ave", "city": "Miami", "country": "United States", "email": "jane_doe@gmail.com", "first_name": "Jane", "last_name": "Doe", "state": "FL", "zip_code": "03423",

我已经阅读了类似的文章,但在我的案例中仍然需要一些帮助…我在从包含键和值对的json数组中获取值时遇到了麻烦:

{"address": "4 Ficticious Ave", 
"city": "Miami", 
"country": "United States", 
"email": "jane_doe@gmail.com", 
"first_name": "Jane", 
"last_name": "Doe", 
"state": "FL", 
"zip_code": "03423", 
"response_data": 
"[{"key":"7122", "value":"37-52"},
{"key":"7123","value":"Female"},
{"key":"7124","value":"$35,000 to $50,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}
我已尝试获取所有值,但均未成功,尤其是对于响应|数据键|值对:

{"address": "4 Ficticious Ave", 
"city": "Miami", 
"country": "United States", 
"email": "jane_doe@gmail.com", 
"first_name": "Jane", 
"last_name": "Doe", 
"state": "FL", 
"zip_code": "03423", 
"response_data": 
"[{"key":"7122", "value":"37-52"},
{"key":"7123","value":"Female"},
{"key":"7124","value":"$35,000 to $50,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}
`//从$DATA数组获取JSON数据

// Identify the content as json
header("Content-Type: application/json; charset=UTF-8");

// get the contents of the JSON file
$data = file_get_contents("php://input");

//decode JSON data to PHP array
$content = json_decode($data, true);

//Fetch the details of customer
$Cust_Fname = $content['first_name'];
$Cust_Lname = $content['last_name'];
$Cust_Email = $content['email'];
$Street_Address = $content['address'];
$City = $content['city'];
$State = $content['state'];
$Country = $content['country'];
$Postal_Code = $content['zip_code'];

//also fetch the appended "array" of key/value fields...
$Response_AgeKey = $content['reponse_data'][0]['key'];
$Response_GenderKey = $content['reponse_data'][1]['key'];
$Response_IncomeKey = $content['reponse_data'][2]['key'];
$Response_CityKey = $content['reponse_data'][3]['key'];
$Response_StateKey = $content['reponse_data'][4]['key'];
$Response_CountryKey = $content['reponse_data'][5]['key'];
$Response_Age = $content['reponse_data'][0]['value'];
$Response_Gender = $content['reponse_data'][1]['value'];
$Response_Income = $content['reponse_data'][2]['value'];
$Response_City = $content['reponse_data'][3]['value'];
$Response_State = $content['reponse_data'][4]['value'];
$Response_Country = $content['reponse_data'][5]['value']
`响应数据不是有效的JSON。数组中有一对额外的双引号。删除包含的双引号,这样它就可以工作了

{
  "address": "4 Ficticious Ave",
  "city": "Miami",
  "country": "United States",
  "email": "jane_doe@gmail.com",
  "first_name": "Jane",
  "last_name": "Doe",
  "state": "FL",
  "zip_code": "03423",
  "response_data": [
    {
      "key": "7122",
      "value": "37-52"
    },
    {
      "key": "7123",
      "value": "Female"
    },
    {
      "key": "7124",
      "value": "$35,000 to $50,000 USD"
    },
    {
      "key": "6176",
      "value": "Miami"
    },
    {
      "key": "6177",
      "value": "FL"
    },
    {
      "key": "6179",
      "value": "United States"
    }
  ]
}

正如有人所说,响应数据不是有效的JSON。数组中有一对额外的双引号。但我认为,如果它是一个响应,而不是手动键入的json,则最好在使用json_decode解码之前自动规范化接收到的字符串,主要是通过删除所包含的双引号。您可以使用以下代码来实现:

<?php
 // Identify the content as json
header("Content-Type: application/json; charset=UTF-8");

// get the contents of the JSON file
$data = file_get_contents("php://input");

//normalize the json in order to be properly decoded

$start=strpos($data,':',strpos($data,'response_data'));
$get=substr($data,$start+1,strrpos($data,'"')-$start);
$data=str_replace($get,trim(trim($get),'"'),$data);

//decode JSON data to PHP array
$content = json_decode($data, true);

//Fetch the details of customer

$Cust_Fname = $content['first_name'];
$Cust_Lname = $content['last_name'];
$Cust_Email = $content['email'];
$Street_Address = $content['address'];
$City = $content['city'];
$State = $content['state'];
$Country = $content['country'];
$Postal_Code = $content['zip_code'];

//also fetch the appended "array" of key/value fields...
$Response_AgeKey = $content['reponse_data'][0]['key'];
$Response_GenderKey = $content['reponse_data'][1]['key'];
$Response_IncomeKey = $content['reponse_data'][2]['key'];
$Response_CityKey = $content['reponse_data'][3]['key'];
$Response_StateKey = $content['reponse_data'][4]['key'];
$Response_CountryKey = $content['reponse_data'][5]['key'];
$Response_Age = $content['reponse_data'][0]['value'];
$Response_Gender = $content['reponse_data'][1]['value'];
$Response_Income = $content['reponse_data'][2]['value'];
$Response_City = $content['reponse_data'][3]['value'];
$Response_State = $content['reponse_data'][4]['value'];
$Response_Country = $content['reponse_data'][5]['value']

?>

谢谢,多留一双眼睛总是有帮助的!好的,代码运行时没有错误,数据作为新记录插入MySQL中[未提供代码]。奇怪的是,没有一个来自response_data键值对的数据显示出来……所有这些列都是空白的。我想我得好好玩玩这个检索语法。非常感谢…我现在走得更远了。谢谢这个…它帮助我获得了@elemental提供的删除代码。