首页 > 解决方案 > php json_decode 将数据库 json 字符串视为对象

问题描述

我正在使用json_encode将数组转换为 JSON,然后存储在 MySQL 表中。在尝试对 MySQL 表中的数据执行 json_decode 时,我收到一个错误,即传递给 json_decode 的参数是一个 object

"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()在将数据存储到数据库之前,我是否需要用引号括起来?

将 Laravel 5.7 与 php 7.3.2 一起使用

更新:这是将数组转换为 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);
}

标签: phplaravel

解决方案


推荐阅读