如何正确解析 json 对象?

How to parse json object properly?

我正在尝试解析此 JSON 对象并遍历它以在 html 中生成 table。在试验中,我无法 属性 回应这些值。我该怎么做才正确?

{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""},"2":{"1":"12435800","2":"20160714","3":"0318","4":"12435800","5":"","6":"DBQ","7":"QA79DOHB021104613880010010000","8":"201606","9":"183941.22166664","10":"113","11":"","12":"","13":"","14":"","15":""},"3":{"1":"Record+ID","2":"Employee+QID","3":"Employee+Visa+ID","4":"Employee+Name","5":"Employee+Bank+Short+Name","6":"Employee+Account","7":"Salary+Frequency","8":"Number+of+Working+Days","9":"Net+Salary","10":"Basic+Salary","11":"Extra+hours","12":"Extra+Income","13":"Deductions","14":"Payment+Type","15":"Notes/+Comments"},"4":{"1":"1","2":"27835620341","3":"","4":"SHIJAN+THARAKAN+THOMAS","5":"DBQ","6":"2025","7":"M","8":"30","9":"7300","10":"5000","11":"0.00","12":"2500.00000000","13":"200","14":"","15":""}}

我试过了:

$data = json_decode($_POST['data'],true);

//echo count($data);

echo ($data[4][2]);`

结果是 null JSON。我怎样才能正确地做到这一点?

您的 JSON 字符串是 -

$str = '{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""},"2":{"1":"12435800","2":"20160714","3":"0318","4":"12435800","5":"","6":"DBQ","7":"QA79DOHB021104613880010010000","8":"201606","9":"183941.22166664","10":"113","11":"","12":"","13":"","14":"","15":""},"3":{"1":"Record+ID","2":"Employee+QID","3":"Employee+Visa+ID","4":"Employee+Name","5":"Employee+Bank+Short+Name","6":"Employee+Account","7":"Salary+Frequency","8":"Number+of+Working+Days","9":"Net+Salary","10":"Basic+Salary","11":"Extra+hours","12":"Extra+Income","13":"Deductions","14":"Payment+Type","15":"Notes/+Comments"},"4":{"1":"1","2":"27835620341","3":"","4":"SHIJAN+THARAKAN+THOMAS","5":"DBQ","6":"2025","7":"M","8":"30","9":"7300","10":"5000","11":"0.00","12":"2500.00000000","13":"200","14":"","15":""}}';

$result_array = json_decode($str, true);
echo '<pre>'; print_r($result_array);
echo $result_array[4][2];

对我来说效果很好。请检查您的 $_POST['data'] 变量。如果定义了这个 data 索引,它可能 undefined 通过 print_r($_POST); 调试这个变量,你会看到一个数据索引,例如 -

Array
(
   ['data'] => 'some_value',
)