Php Codeigniter-post中json对象的数组
在FireBug中,我查看我的jquery post请求参数,如下所示Php Codeigniter-post中json对象的数组,php,jquery,json,codeigniter,Php,Jquery,Json,Codeigniter,在FireBug中,我查看我的jquery post请求参数,如下所示 adults 1 applicants[] [object Object] attendees 1 children 0 ({attendees:"2", adults:"2", children:"0", grptype:"2", 'applicants[]':[{firstname:"John", lastname:"Doe", age:"33", alle
adults 1
applicants[] [object Object]
attendees 1
children 0
({attendees:"2",
adults:"2",
children:"0",
grptype:"2",
'applicants[]':[{firstname:"John", lastname:"Doe", age:"33", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}, {firstname:"Jane", lastname:"Doe", age:"34", allergies:"true", diabetic:"false", lactose:"false", note:"pollen"}]
})
adults 2
applicants[] {firstname:"John", lastname:"Doe", age:"23", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}
applicants[] {firstname:"Jane", lastname:"Doe", age:"23", allergies:"false", diabetic:"false", lactose:"false", note:""}
attendees 2
children 0
adults 1
applicants[] {"firstname": "John", "lastname": "Doe", "age": "34", "allergies": "true", "diabetic": "true", "lactose": "false", "note": "nuts"}
attendees 1
children 0
在这篇post请求中,名为appenders的数组包含json对象,我希望迭代该对象并从codeigniter控制器中提取值。json字符串可能如下所示
adults 1
applicants[] [object Object]
attendees 1
children 0
({attendees:"2",
adults:"2",
children:"0",
grptype:"2",
'applicants[]':[{firstname:"John", lastname:"Doe", age:"33", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}, {firstname:"Jane", lastname:"Doe", age:"34", allergies:"true", diabetic:"false", lactose:"false", note:"pollen"}]
})
adults 2
applicants[] {firstname:"John", lastname:"Doe", age:"23", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}
applicants[] {firstname:"Jane", lastname:"Doe", age:"23", allergies:"false", diabetic:"false", lactose:"false", note:""}
attendees 2
children 0
adults 1
applicants[] {"firstname": "John", "lastname": "Doe", "age": "34", "allergies": "true", "diabetic": "true", "lactose": "false", "note": "nuts"}
attendees 1
children 0
看看上面的申请者[],我有两个人的信息作为json对象。我不确定如何访问控制器中的数据。看到这个了吗
$applicants = $this->input->post('applicants');
$this->output->append_output("<br/>Here: " . $applicants[0].firstname );
现在我仍然得到一个错误,说
**Message: json_decode() expects parameter 1 to be string, array given**
有什么想法吗
编辑2
好的,现在是这样的
adults 1
applicants[] [object Object]
attendees 1
children 0
({attendees:"2",
adults:"2",
children:"0",
grptype:"2",
'applicants[]':[{firstname:"John", lastname:"Doe", age:"33", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}, {firstname:"Jane", lastname:"Doe", age:"34", allergies:"true", diabetic:"false", lactose:"false", note:"pollen"}]
})
adults 2
applicants[] {firstname:"John", lastname:"Doe", age:"23", allergies:"true", diabetic:"true", lactose:"false", note:"nuts"}
applicants[] {firstname:"Jane", lastname:"Doe", age:"23", allergies:"false", diabetic:"false", lactose:"false", note:""}
attendees 2
children 0
adults 1
applicants[] {"firstname": "John", "lastname": "Doe", "age": "34", "allergies": "true", "diabetic": "true", "lactose": "false", "note": "nuts"}
attendees 1
children 0
在控制器id中执行此操作
$applications = $this->input->post('applicants');
foreach ( $applications as $item)
{
$item = json_decode($item, true);
$this->output->append_output(print_r($item));
}
这就是这种逻辑的结果
Array
(
[firstname] => John
[lastname] => Doe
[age] => 34
[allergies] => true
[diabetic] => true
[lactose] => false
[note] => nuts
)
不确定我做错了什么,无论我如何访问dater,我都会收到一个错误,大意是我无法像那样访问它。如何提取值?试试这个plz
$applicants = $this->input->post('applicants');
$json_output = json_decode($applicants );
foreach ( $json_output as $person)
{
$this->output->append_output("<br/>Here: " . $person->firstname );
}
您必须在服务器上使用
$applications = json_decode($this->input->post('applicants'), true);
因此,它将成为一个关联数组,您可以像使用数组一样使用它,而json
中的第二个参数(true)将被转换为对象。在解码之前,它只是一个字符串
(json/java脚本对象表示法
string)
更新:因为它已经是一个对象数组,所以您不需要使用json\u decode
,只需在视图中循环数组即可
foreach($applicants as $item)
{
echo $item->firstname . '<br />';
echo $item->lastname . '<br />';
// ...
}
foreach(申请人作为$item)
{
echo$item->firstname.“
”;
echo$item->lastname.“
”;
// ...
}
根据编辑2,它应该作为数组访问
echo $item['firstname'] . '<br />'
echo$item['firstname']
'
您在帖子中看到的[object object]
意味着这正是要发送到服务器的内容-一个字符串[object object]
。如何创建post数据?我在widard中有两个表单,在提交之前,我为每个表单创建json,并将json对象与jquery extend()合并。然后我用jquert$.post(blablabla)发送。t=您是说将其作为字符串而不是对象发送吗?是的,您应该先将javascript对象转换为JSON字符串,然后再发送该字符串。然后,像@Sheikh Herra指出的那样,在服务器上解码JSON字符串。真不敢相信我这么笨,我用$item['firstname']来提取值。谢谢大家的指导。我一直收到一个错误,json_解码需要一个字符串,我给它一个数组。我做了一个打印,它刚刚输出了“object”。我正在发送一个json对象数组,该数组应该是字符串吗?@Binaryrespawn,那么你不需要使用json\u decode
,它已经是一个对象数组,只需循环它。我按照上面的编辑2所述调整了我的json字符串。并使用php数组表示法而不是像$items['firstname']这样的对象表示法来提取值。这很有效。谢谢你的帮助。@Binaryrespawn,很高兴知道这一点。