如何使用 php 引用具有键值对的 json 数组
How to reference a json array having key value pairs using php
我已经通读了类似的帖子,但在我的案例中仍然可以使用一些帮助...我有一段时间从包含键和值对的 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":",000 to ,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}
我试图获取所有值,但一直没有成功,尤其是 response_data 键|值对:
`// 从 $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']
`
"response_data"
无效 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": ",000 to ,000 USD"
},
{
"key": "6176",
"value": "Miami"
},
{
"key": "6177",
"value": "FL"
},
{
"key": "6179",
"value": "United States"
}
]
}
正如有人所说 "response_data" 无效 JSON。有一对额外的双引号括起 array.But 我认为如果它是响应而不是手动输入的 json 最好在使用 [=15= 解码之前自动规范化接收到的字符串] 主要是通过移除封闭的double。您可以使用下面的代码来实现它:
<?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']
?>
我已经通读了类似的帖子,但在我的案例中仍然可以使用一些帮助...我有一段时间从包含键和值对的 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":",000 to ,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}
我试图获取所有值,但一直没有成功,尤其是 response_data 键|值对:
`// 从 $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']
`
"response_data"
无效 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": ",000 to ,000 USD"
},
{
"key": "6176",
"value": "Miami"
},
{
"key": "6177",
"value": "FL"
},
{
"key": "6179",
"value": "United States"
}
]
}
正如有人所说 "response_data" 无效 JSON。有一对额外的双引号括起 array.But 我认为如果它是响应而不是手动输入的 json 最好在使用 [=15= 解码之前自动规范化接收到的字符串] 主要是通过移除封闭的double。您可以使用下面的代码来实现它:
<?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']
?>