[英]Codeigniter - array of json objects in post
In fire bug I view my jquery post request parameters, its like this 在火灾中,我查看了我的jquery帖子请求参数,如下所示
adults 1
applicants[] [object Object]
attendees 1
children 0
In this post request the array called applicants contains json object which I want ti iterate over and pull out values in my codeigniter controller. 在这篇文章请求中,称为“ 申请人”的数组包含json对象,我希望对其进行迭代并在我的代码初始化控制器中提取值。 The json string may look like this json字符串可能看起来像这样
({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"}]
})
Look at the applicants[] above, see I have info for two people as a json object. 看看上面的applicants [],请参阅我有两个人的信息作为json对象。 I am not sure how access the data in my controller. 我不确定如何访问控制器中的数据。 see this 看到这个
$applicants = $this->input->post('applicants');
$this->output->append_output("<br/>Here: " . $applicants[0].firstname );
I was thinking $applicants[0] woild refer to the json object and I could just pull outs values on demand. 我当时以为$ applicants [0]会引用json对象,我可以按需提取值。 Not sure hat I am doing wrong. 不确定我做错了什么。 Thanks guys. 多谢你们。
EDIT So I have adjusted my json and it looks like this 编辑所以我已经调整了我的json,它看起来像这样
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
Now I am still getting an error saying 现在我仍然收到错误消息
**Message: json_decode() expects parameter 1 to be string, array given**
Any ideas ? 有任何想法吗 ?
EDIT 2 编辑2
Ok mu data now liiks like this 好的亩数据现在喜欢这样
adults 1
applicants[] {"firstname": "John", "lastname": "Doe", "age": "34", "allergies": "true", "diabetic": "true", "lactose": "false", "note": "nuts"}
attendees 1
children 0
In the controller id did this 在控制器ID中做到了这一点
$applications = $this->input->post('applicants');
foreach ( $applications as $item)
{
$item = json_decode($item, true);
$this->output->append_output(print_r($item));
}
This is the result of that logic 这是这种逻辑的结果
Array
(
[firstname] => John
[lastname] => Doe
[age] => 34
[allergies] => true
[diabetic] => true
[lactose] => false
[note] => nuts
)
Not sure what I am doing wrong, no matter what I do to access the dater I get an error to the effect that I cannot access it like that. 不确定我做错了什么,无论我如何访问约会者,都会收到一条错误消息,提示我无法像这样访问它。 How do I pull out the values ? 如何提取值?
You have to decode it on the server using 您必须使用以下命令在服务器上对其进行解码
$applications = json_decode($this->input->post('applicants'), true);
So, it'll become an associative array and you can use it like an array
, without the second argument (true) in json_decode the json
will be converted to an object. 因此,它将成为一个关联数组,您可以像使用array
一样使用它,而在json_decode中没有第二个参数(true)的情况下 , json
将被转换为对象。 Until you decode it, it's only a string
( json/java script object notation
string). 在解码之前,它只是一个string
( json/java script object notation
字符串)。
Update: Since it's already an array of objects then you don't need to use json_decode
, just loop the array in your view
like 更新:由于它已经是对象数组,因此您无需使用json_decode
,只需在view
循环数组即可,例如
foreach($applicants as $item)
{
echo $item->firstname . '<br />';
echo $item->lastname . '<br />';
// ...
}
According to the edit 2 it should be access as an array 根据编辑2,它应该作为数组访问
echo $item['firstname'] . '<br />'
try this plz 试试这个PLZ
$applicants = $this->input->post('applicants');
$json_output = json_decode($applicants );
foreach ( $json_output as $person)
{
$this->output->append_output("<br/>Here: " . $person->firstname );
}
or 要么
$json_output = json_decode($applicants,TRUE );
echo $json_output[0][firstname] ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.