【发布时间】:2019-02-11 09:53:38
【问题描述】:
以下 json 数组数据被提取到 php 脚本中,最终在 MySQL 数据库中创建记录:
{"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"}]"}
我得到了以下 php 脚本来从 json 数组中获取数据并通过插入数据在 MySQL 数据库中创建记录。但是,除了 response_data 键|值对中的数据之外的所有数据都会被填充——相关的 MySQL 列都是空的:
// 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");
//this normalize routine was provided by @Elementary in
// response to my request on Stack Overflow 09052018...
//begin 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);
//end normalize
//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 的数据除外。考虑到我的语法可能有问题,我尝试将 response_data 变量替换为:
//try this syntax instead…
$Response_AgeKey = $content['reponse_data']['key'][0];
$Response_GenderKey = $content['reponse_data']['key'][1];
$Response_IncomeKey = $content['reponse_data']['key'][2];
$Response_CityKey = $content['reponse_data']['key'][3];
$Response_StateKey = $content['reponse_data']['key'][4];
$Response_CountryKey = $content['reponse_data']['key'][5];
$Response_Age = $content['reponse_data']['value'][0];
$Response_Gender = $content['reponse_data']['value'][1];
$Response_Income = $content['reponse_data']['value'][2];
$Response_City = $content['reponse_data']['value'][3];
$Response_State = $content['reponse_data']['value'][4];
$Response_Country = $content['reponse_data']['value'][5];
获得了相同的结果——在 MySQL 数据库中创建了一条记录,但 response_data 数组字段不填充关联的 MySQL 列。我可以使用帮助来学习其他方法来识别和获取 response_data 数组中的数据。请注意,我不想将 response_data 数组作为 json 数组插入 MySQL —— 相反,数组中的数据应该进入关联的 MySQL 列!
【问题讨论】:
-
您不必规范化有效的 json。谁创建了这个 JSONString,你还是你从其他地方得到的。
-
您好,json 数据来自一个 webhook,该 webhook 在另一台服务器上捕获用户输入。如果没有规范化,记录就不会在 MySQL 中创建。
-
reponse!==response所以这只是一个错字,你的第一次尝试会在你正确拼写的时候起作用 -
是的,我意识到 JSON 是无效的,但它不应该是,我想说如果是你在创建它,你也应该发布该代码,我们可以将 JSONString 生成修复为好
标签: php json mysqli associative-array keyvaluepair