跨语言交换数据:
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
联系&投稿:service@yojoygroup.com
原文地址:http://11834143.blog.51cto.com/11824143/1826766