这周主要学习了JSON与jQuery的一些基本内容:
1.JSON的定义:
var jsonObj={ "key1":12,//以键值对的形式出现,键勇双引号,两个键值对键用逗号隔开。 "key2":"abc",//值的类型可以为,字符型,整型,浮点型,布尔型,也可以为JSON "key3":true };
JSON的访问用对象名.键名访问。Json的两种存在形式:以对象的形式存在,以字符串的形式存在。
用JSON.stringify()可以将对象转为字符串,JSON.parse()可以将字符串转为对象(常用于客户端与服务端进行信息传递)
2..javaBean与json的互传,用到gson.jar包,使用GSON.toJson()方法
json转为javaBean对象使用GSON.fromJson(String,class),String为Json字符串,class为javaBean
对于List与Json的转化,List转为json字符串与javaBean类似,单使用GSON.formJson(),不能传class对象
谣传一个Type,要新建一个类继承TypeToKen<>,然后调用此类的getType()方法。为了节约资源可使用匿名内部类的方法:
List<javatest> list2=new ArrayList<javatest>();
list2=gson.fromJson(str2,new TypeToken<ArrayList<javatest>>(){}.getType());
Map与Json的转化与List一样。
3.
$是JQuery的核心函数,能完成JQuery的很多功能,$()就是调用$这个函数、
1.当传入参数为函数时:
表示页面加载完成之后,相当于window.onload=function(){}
2.传入参数为HTML字符串时:
会创建这个HTML对象。
3.当传入参数为选择器字符串时:
$("#id属性值")id选择器根据id查询标签对象。
$("标签名" )标签名选择器,根据指定的标签名查询标签对象。
$(".class属性值") 类选择器 根据class属性值查询标签对象。
4.当传入的参数为DOM对象时
会把这个dom对象转为JQuery对象