[学习实践]jquery对多维json串输出示例,eval使用等,实践过

jackxiang 2011-2-28 23:40 | |
1.jquery添加
2.json eval,输出json二级用点来:var info=anObjectEval.info.info;
3.循环输出
对json用点,可以通过json树转为树形后,在根据显示的树形节点图层级关系通过点来实现!!!

如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<SCRIPT language="javascript" src="./jquery.js"></SCRIPT>
<script language="javascript1.1">
$(document).ready(function(){
       // var anObject = {one:1,two:2,three:3};//对json数组each
       var anObject = {"result":0,"info":{"p":1,"size":15,"total":602,"cnt":15,"info":[{"uin":"1024171160","nick":"\u738b\u661f","name":"wx1024171120","head":"http:\/\/t0.qlogo.cn\/mbloghead\/71be2fe45a13bcdcf560"},{"uin":"409795996","nick":"\u9648\u7d20\u83b9","name":"txchenjia","head":"http:\/\/t0.qlogo.cn\/mbloghead\/d7ec8a17dd9b5516f9ba"},{"uin":"79141993","nick":"\u56f4\u8116\u9152\u5427","name":"smfox528","head":"http:\/\/t1.qlogo.cn\/mbloghead\/26484db736b1ef94cb5c"},{"uin":"625655093","nick":"\u8c0c\u7ecd\u5dcd","name":"slydelo","head":"http:\/\/t1.qlogo.cn\/mbloghead\/4f9ab3af0f63ec4a58da"},{"uin":"1460481684","nick":"\u4e92\u8054\u7f51\u5e7f\u544a\u6742\u8c08","name":"IT_ads","head":"http:\/\/t0.qlogo.cn\/mbloghead\/3b02f83a2dd50c74d076"},{"uin":"611986336","nick":"\u5e7f\u544a\u5708\u90a3\u70b9\u4e8b","name":"guanggaoquan","head":"http:\/\/t0.qlogo.cn\/mbloghead\/5fe3f56dd24c819bd84a"},{"uin":"475119688","nick":"\u7ea2\u67ab","name":"hongfeng0429","head":"http:\/\/t0.qlogo.cn\/mbloghead\/c840f74f3bfb6bc902b0"},{"uin":"1013787","nick":"\u5510\u84c9","name":"rr-one","head":"http:\/\/t3.qlogo.cn\/mbloghead\/7a61233bff84974dcf22"},{"uin":"138188661","nick":"\u59dc\u78ca","name":"tomjiang","head":"http:\/\/t1.qlogo.cn\/mbloghead\/b2ce2ae5bdbb83cf60d4"},{"uin":"10622998","nick":"\u7f57\u601d","name":"roseluo12","head":"http:\/\/t2.qlogo.cn\/mbloghead\/0f62b2a3cd12da399412"},{"uin":"88299936","nick":"\u9648\u51e1\u51e1","name":"chenfanfan","head":"http:\/\/t0.qlogo.cn\/mbloghead\/d2e59014e588c7c31cba"},{"uin":"344739953","nick":"\u4efb\u96ef\u96ef","name":"amberwen","head":"http:\/\/t1.qlogo.cn\/mbloghead\/172172c5608cb06ece82"},{"uin":"21888","nick":"amanda","name":"amanda11","head":"http:\/\/t0.qlogo.cn\/mbloghead\/3075c133c8e6f386ad7a"},{"uin":"826237334","nick":"\u9752\u86d9-PP","name":"frog--","head":""},{"uin":"27017202","nick":"\u8d75\u4e9a\u6960","name":"mscomm","head":"http:\/\/t2.qlogo.cn\/mbloghead\/5c2d5c72749010dd66c0"}]}};

       var anObjectEval=evalJson(anObject);
       var info=anObjectEval.info.info;
        $.each(info,function(name,value) {
            document.write("<hr>");
          document.write(info[name].uin);
            document.write("<p>");
            document.write(info[name].nick);
            document.write("<p>");
            document.write(info[name].head);
             document.write("<p>");
        });



});
function evalJson( jsonText )
{/*{{{*/
    var jsonObject;
    switch(typeof jsonText ){
    case 'string':
        try{
            jsonObject = eval('('+jsonText+')');
        }
        catch(e){
            jsonObject = null;
        }
        break;
    case 'object':
    case 'number':
    case 'boolean':
    case 'function':
        jsonObject = jsonText;
    }
    return jsonObject;
}/*}}}*/


</script>
<body>

</body>

特别注意:

$(document).ready(function() {
  // Handler for .ready() called.
});

后面是: }); ,这个是函数的},ready的),函数在ready里面,所以:});,分号也别忘记了。


Js转字符串示例:



Flash转盘Ajax调用后对返回串转为Json类:

直接用Jquery返回时就已经让Jquery帮忙转为了Json的代码,注意给Flash用时需要有一个全局变量,在success里面把返回的data这个Json对象给jsonObject后,在Success函数外面进行return jsonObject ,如下:

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/4074/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2011-6-15 15:23
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]