如何JSON转换成数组,遍历它的jQuery? [英] How to convert JSON to array and loop over it in jQuery?
问题描述
我使用JSON与用户进行沟通。 PHP数组转换为JSON这种形式:
{成功:文本到显示器,警告:NONE,通知:文本到显示器,错误:没有}
jQuery的显示通知:
函数回调(数据){
如果(data.notice!=='无'){
displayNotice(data.notice);
}
如果(data.success!=='无'){
displaySuccess(data.success);
}
如果(data.warning!=='无'){
displayWarning(data.warning);
}
如果(data.error!=='无'){
displayError(data.error);
}
}
不幸的是,这种方法不能显示两个错误或两个通知或两个警告,因为新的语句来替换旧的声明。
< PHP
$ uwaga ['错误'] ='老言';
$ uwaga ['错误'] ='新的语句';
//只显示新说法
回声json_en code($ uwaga);
?>
我认为,使用数组:
< PHP
$ uwaga =阵列();
$ uwaga [1] =阵列('类型'= GT;'通知','文字'= GT;旧表);
$ uwaga [2] =阵列('类型'= GT;'通知','文字'= GT;新表);
//显示新说法,老言
//生成:{1:{类型:通知,文:老说法},2:{类型:通知,文:新说法} }
回声json_en code($ uwaga);
?>
如何翻译这个PHP code jQuery的(主要是:???如何JSON对象转换为数组如何循环使用如何使用这个循环中如何引用 $ uwaga [$关键] [ '名字']
和 $ uwaga [$关键] ['文本'])
?
的foreach($ uwaga为$关键=> $值){
开关($ uwaga [$关键] ['名']){
案警告:
displayWarning($ uwaga [$关键] ['文本']);
打破;
}}
而不是这样的:
$ uwaga =阵列();
$ uwaga [1] =阵列('类型'= GT;'通知','文字'= GT;旧表);
$ uwaga [2] =阵列('类型'= GT;'通知','文字'= GT;新表);
只是这样做,没有指数:
$ uwaga =阵列();
$ uwaga [] =阵列('类型'= GT;'通知','文字'= GT;旧表);
$ uwaga [] =阵列('类型'= GT;'通知','文字'= GT;新表);
这将在数组末尾将它们分配指数(从零,没有之一)。
然后采取所有这些:
如果(data.notice!=='无'){
displayNotice(data.notice);
}
如果(data.success!=='无'){
displaySuccess(data.success);
}
如果(data.warning!=='无'){
displayWarning(data.warning);
}
如果(data.error!=='无'){
displayError(data.error);
}
...在jQuery的块将它们包装每()
作为罗布建议。然后,它会成为(假设数据是 json.messages
)
$。每个(json.messages,功能(索引,数据){
如果(data.notice!=='无'){
displayNotice(data.notice);
}
如果(data.success!=='无'){
displaySuccess(data.success);
}
如果(data.warning!=='无'){
displayWarning(data.warning);
}
如果(data.error!=='无'){
displayError(data.error);
}
});
I am using JSON to communicate with the user. PHP converts array to JSON to this form:
{"success":"text-to-display","warning":"NONE","notice":"text-to-display","error":"NONE"}
jQuery display notification:
function callback (data){
if(data.notice !== 'NONE'){
displayNotice(data.notice);
}
if(data.success !== 'NONE'){
displaySuccess(data.success);
}
if(data.warning !== 'NONE'){
displayWarning(data.warning);
}
if(data.error !== 'NONE'){
displayError(data.error);
}
}
Unfortunately, in this method can't display two error or two notice or two warning, because new statement replace old statement.
<?php
$uwaga['error'] = 'old statement';
$uwaga['error'] = 'new statement';
// display only "new statement"
echo json_encode($uwaga);
?>
I think that use array:
<?php
$uwaga = array();
$uwaga[1] = array('type' => 'notice', 'text' => 'old statement');
$uwaga[2] = array('type' => 'notice', 'text' => 'new statement');
// display "new statement" and "old statement"
// generate: {"1":{"type":"notice","text":"old statement"},"2": {"type":"notice","text":"new statement"}}
echo json_encode($uwaga);
?>
How "translate" this PHP code on jQuery (mainly: how convert json object to array? how loop use? how using this loop? How refer to $uwaga[$key]['name']
and $uwaga[$key]['text'])
?
foreach ($uwaga as $key => $value) {
switch ($uwaga[$key]['name']) {
case 'warning':
displayWarning($uwaga[$key]['text']);
break;
}}
Rather than this:
$uwaga = array();
$uwaga[1] = array('type' => 'notice', 'text' => 'old statement');
$uwaga[2] = array('type' => 'notice', 'text' => 'new statement');
Just do this, without indices:
$uwaga = array();
$uwaga[] = array('type' => 'notice', 'text' => 'old statement');
$uwaga[] = array('type' => 'notice', 'text' => 'new statement');
That will assign them indices (from zero, not one) at the end of the array.
Then take all of these:
if(data.notice !== 'NONE'){
displayNotice(data.notice);
}
if(data.success !== 'NONE'){
displaySuccess(data.success);
}
if(data.warning !== 'NONE'){
displayWarning(data.warning);
}
if(data.error !== 'NONE'){
displayError(data.error);
}
... and wrap them in a block of jQuery's each()
as Rob recommends. It will then become (assuming the data is in json.messages
):
$.each(json.messages, function (index, data) {
if(data.notice !== 'NONE'){
displayNotice(data.notice);
}
if(data.success !== 'NONE'){
displaySuccess(data.success);
}
if(data.warning !== 'NONE'){
displayWarning(data.warning);
}
if(data.error !== 'NONE'){
displayError(data.error);
}
});
这篇关于如何JSON转换成数组,遍历它的jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!