我正在使用 json_encode 将数组转换为JSON,然后存储在MySQL表中。尝试对MySQL表中的数据执行json_decode时,我收到一个错误,即传递给json_decode的自变量是一个对象。
“
json_decode()
期望参数1为字符串,给定对象”
这是存储在MySQL表中的JSON字符串:
{
"CreditScoreFactor":"{\"98\":\" Make all future payments on time. The impact on your credit score from the bankruptcy will diminish over time.\",\"negative_factors\":[\" There is a bankruptcy on your credit report\",\" The balances on your accounts are too high compared to loan amounts\",\" Lack of sufficient relevant real estate account information\",\" You have either very few loans or too many loans with recent delinquencies\"],\"04\":\" Paying down the balances on your accounts will benefit your score.\",\"63\":\" Maintaining open and active credit accounts in good standing can help improve your credit score.\",\"08\":\" Paying bills on time every month is important to maintaining a good credit score. If you remain behind with any payments, bring them current as soon as possible, and then make future payments on time. Over time, this will have a positive impact on your score.\"}",
"TotalBalances":"5039",
"TotalMonthlyPayments":"57",
"TotalAccounts":"4",
"OpenAccounts":"3",
"CloseAccounts":"1",
"DelinquentAccounts":"0",
"DerogatoryAccounts":"3",
"PublicRecords":null,
"Utilization":null,
"OnTimePaymentPercentage":null,
"BorrowerName":[
{
"first":"SOOR",
"middle":"R",
"last":"DOOR",
"InquiryDate":"2019-03-06"
}
],
"BorrowerBirth":[
{
"date":null,
"InquiryDate":[
"2019-03-06"
]
}
],
"previousAddresses":[
{
"dateReported":"2006-09-28",
"InquiryDate":[
"2019-03-06"
],
"address":{
"city":"SCOTTSDALE",
"direction":"N",
"houseNumber":"1001",
"postDirection":"",
"streetName":"27",
"stateCode":"AK",
"streetType":"PL",
"unit":"105",
"postalCode":"85257",
"type":"previous"
}
},
{
"dateReported":null,
"InquiryDate":[
"2019-03-06"
],
"address":{
"city":"PHOENIX",
"direction":"E",
"houseNumber":"4202",
"postDirection":"",
"streetName":"CACTUS",
"stateCode":"AZ",
"streetType":"RD",
"unit":"4101",
"postalCode":"85032",
"type":"previous"
}
},
{
"dateReported":"2007-10-09",
"InquiryDate":[
"2019-03-06"
],
"address":{
"city":"CALIFORNIA",
"direction":"N",
"houseNumber":"767",
"postDirection":"",
"streetName":"DAVID",
"stateCode":"AZ",
"streetType":"CT",
"unit":"",
"postalCode":"85226",
"type":"current"
}
}
],
"employer":[
{
"emp_updatedon":"2017-12-22",
"emp_name":"PROEM PARTY EVENT RENTALS",
"emp_partition":"0"
},
{
"emp_updatedon":"2007-04-27",
"emp_name":"PROFESSIONAL EVENT MNGMNT",
"emp_partition":"1"
}
]
}
我注意到,如果我删除了反斜杠,该错误就会消失。 另外,如果我将数据库值用双引号引起来,错误就会消失。
不确定是什么问题。在将json_encode()
数据存储在数据库中之前,是否需要将它们用引号引起来?
在PHP 7.3.2中使用Laravel 5.7
更新: 这是将数组转换为json的代码
$reportData->extrainfo = !empty($report['extrainfo']) ? json_encode($report['extrainfo'],JSON_UNESCAPED_SLASHES) : null;
$reportData->save();
这是获取数据库值的代码:
if ($extrainfo = $this->extrainfo) {
$extrainfo = json_decode($extrainfo, true);
}