Jquery 对json的增删改: 查询(遍历)

jquery 对得到的json 进行增删改 操作

var ojson={'a':[{'id':'5'},{'id':'6'}],'b':[{'id':'8'},{'id':'9'}]}; 
 

var t=[{'a':1},{'a':2}];

t.push({'a':3});//增
t[0].a=11;//改
t.remove(0);//删


查询:遍历

举例如下:

var people=
{ "userDetails" :
[
  { "sm_uid" : "226" , "sm_user" : "librashengsh" , "sm_uname" : "ss" , "sm_tea_pic" : "TeacBbsPic/20100422094813.jpg" },
  { "sm_uid" : "231" , "sm_user" : "ssssss" , "sm_uname" : "ss" , "sm_tea_pic" : "TeacBbsPic/20100505135408.jpg" },
  { "sm_uid" : "295" , "sm_user" : "yusuke" , "sm_uname" : "ykl" , "sm_tea_pic" : "images/upic.gif" },
  { "sm_uid" : "227" , "sm_user" : "abcdefg" , "sm_uname" : "abc" , "sm_tea_pic" : "TeacBbsPic/20100422154044.jpg" }
  ]};


//下面是普通搜索
for (var i in people[ 'userDetails' ]) {
    if (people[ 'userDetails' ][i].sm_uid=="226" ) {
         alert(people[ 'userDetails' ][i].sm_user);//获得该id下的名字
    }
}



//下面是模糊搜索
for (var i in people[ 'userDetails' ]) {
    if (people[ 'userDetails' ][i].sm_uid.indexOf( '2' ) !=  -1 ) {
         alert(people[ 'userDetails' ][i].sm_uid);
    }
}

你可能感兴趣的:(h5,jQuery)