码迷,mamicode.com
首页 > Web开发 > 详细

JSON

时间:2016-07-15 13:27:14      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

 

跨语言交换数据:

 

              1:xml

 

              2: json

 

JSON(JavaScript Object Notation):

 

              轻量级的数据交换格式,用于在不同的环境中,不同的语言中,像对方传输数据呀!

 

              Client:Html,JS Java:Servlet

 

                                           把后台的数据传递给前台

 

              前台得到JSON               <------------- 把数据转成JSON

 

JSON一般有两种组成形式:

 

              json对象:必须以{}描述!

 

                            var citys = {uname:‘谢海浪‘,sex:‘random‘,age:18};

 

                            alert(citys.uname);

 

              json数组:必须一[]描述!

 

                            var citys = [1,2,3,4,5,6];

 

                            alert(citys[3]); 

 

                            var school = [{uname:‘谢海浪‘,sex:‘random‘,age:18},{uname:‘董冲‘,sex:‘random‘,age:28},{uname:‘董卓‘,sex:‘男‘,age:18}];

 

                            var school = [{uname:‘谢海浪‘,sex:‘random‘,age:18},{uname:‘董冲‘,sex:‘random‘,age:28},{uname:‘董卓‘,sex:‘男‘,age:18}];

 

                            for(var i=0;i<school.length;i++){

 

                                          alert(school[i].sex);

 

                            }

 

                            var school = [{uname:‘谢海浪‘,sex:‘random‘,age:18,girlfriends:[‘凤姐‘,‘黄河穿‘,‘如花‘]},{uname:‘董冲‘,sex:‘random‘,age:28},{uname:‘董卓‘,sex:‘男‘,age:18}];

 

                            //alert(school[0].girlfriends[0]);

 

                            for(var i=0;i<school[0].girlfriends.length;i++){

 

                                          alert(school[0].girlfriends[i]);

 

                            }

 

              字符串转JSON:

 

                            var school = "[{uname:‘谢海浪‘,sex:‘random‘,age:18,girlfriends:[‘凤姐‘,‘黄河穿‘,‘如花‘]},{uname:‘董冲‘,sex:‘random‘,age:28},{uname:‘董卓‘,sex:‘男‘,age:18}]";              

 

                            var json = eval("("+school+")");

 

                            alert(json);

 

Java对象如何转换成JSON:

 

              1:倒包

 

              2:打json!

 

                            打数组,集合

 

                            JSONArray               

 

                            打对象

 

                            JSONObjcet 

 

              UserinfoMapper userinfoMapper = MyBatisUtil.getSession().getMapper(UserinfoMapper.class);

 

                            List userinfos =userinfoMapper.selectByExample(null);

 

                            //JSONArray json = JSONArray.fromObject(userinfos);

 

                            //System.out.println(json);

 

                             

 

                            Userinfo info = userinfoMapper.selectByPrimaryKey(8);

 

                            JSONObject json = JSONObject.fromObject(info);

 

                            System.out.println(json);

 

 

 

 

 

技术分享

优悦集团(深圳)有限公司

悦享科技  乐享生活

微信:YOJOYGROUP

微博: http://weibo.com/YOJOYGROUP

官网:http://www.yojoygroup.com/

联系&投稿:service@yojoygroup.com

技术分享

     

JSON

标签:

原文地址:http://www.cnblogs.com/YOJOYGROUP/p/5672900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!