首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php引用具有键值对的json数组

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

Stack Overflow用户
提问于 2018-09-05 09:43:55
回答 2查看 697关注 0票数 0

我已经阅读过类似的文章,但在我的例子中仍然需要一些帮助……我从包含键和值对的json数组中获取值是一段相当长的时间:

代码语言:javascript
复制
{"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"}]"}

我尝试过获取所有的值,但都没有成功,特别是在response_data键值对中:

`//从$data数组获取JSON数据

代码语言:javascript
复制
// 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']

`

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-05 17:11:58

正如有人说的那样,"response_data“不是有效的JSON。在array.But中有一对额外的双引号,我认为如果它是响应而不是手动键入的json,最好在用Json_decode解码接收到的字符串之前自动规范化它,主要是通过删除封闭的双重.You,可以使用下面的代码来实现它:

代码语言:javascript
复制
<?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']

?>
票数 0
EN

Stack Overflow用户

发布于 2018-09-05 09:48:01

"response_data"不是有效的JSON。数组中有一对额外的双引号。删除包围双引号,然后它应该可以工作。

代码语言:javascript
复制
{
  "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"
    }
  ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52181987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档